[英]Center UIView in center of MainScreen Despite Location of Superview
I have a uiview which can be dragged around. 我有一个uiview可以拖动。 In that UIView I have a subview which I want to be centered on the device screen both height and width.
在该UIView中,我有一个子视图,我想同时在设备屏幕的高度和宽度上居中。 But what ever I try it centers the superview.
但是我尝试过的一切都将超级视图置于中心。 How can I make it that the subview is always at the center of the UIDevice MainScreen despite where the UIView is.
我如何才能使子视图始终位于UIDevice MainScreen的中心,而不管UIView在哪里。
I have (stripped down because its 50000 lines long): 我已经删除(因为其50000行长):
popArtwork = [UIImageView new];
popArtwork.frame = CGRectMake(367, 367, WidgetWidth, WidgetWidth/5.3);
popArtwork.layer.cornerRadius = 10;
popArtwork.layer.masksToBounds = YES;
popArtwork.alpha = 0;
[popArtwork setUserInteractionEnabled:YES];
[add addSubview:popArtwork];
Add is my superview. 添加是我的观点。 My program is a little unusual in the way that it is a music widget for the user's home screen (jailbreak tweak) so they can move it where ever.
我的程序有点不寻常,因为它是用户主屏幕的音乐小部件(越狱调整),因此他们可以将其移动到任何地方。 I want the view popArtwork to always be in the center of the screen despite wherever Add might be.
我希望视图popArtwork始终位于屏幕的中心,无论添加在哪里。
I don't know if it's what you are looking for, but its how to center a subview when the superview is re-positioned: 我不知道这是否是您要查找的内容,但是当重新定位超级视图时,如何将子视图居中:
- (void)viewDidLoad {
[super viewDidLoad];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(paned:)];
[_add addGestureRecognizer:pan];
}
-(void)paned:(UIPanGestureRecognizer *)pan{
CGPoint translatedPoint = [(UIPanGestureRecognizer*)pan translationInView:self.view];
if ([(UIPanGestureRecognizer*)pan state] == UIGestureRecognizerStateBegan) {
firstX = [[pan view] center].x;
firstY = [[pan view] center].y;
}
translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y);
[[pan view] setCenter:translatedPoint];
CGRect newSubviewFrame = _subview.frame;
newSubviewFrame.origin.x = [UIScreen mainScreen].bounds.size.width/2.0 - newSubviewFrame.size.width/2.0 - _add.frame.origin.x;
newSubviewFrame.origin.y = [UIScreen mainScreen].bounds.size.height/2.0 - newSubviewFrame.size.height/2.0 - _add.frame.origin.y;
_subview.frame = newSubviewFrame;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.