繁体   English   中英

尽管有Superview的位置,但UIView仍位于MainScreen的中心

[英]Center UIView in center of MainScreen Despite Location of Superview

我有一个uiview可以拖动。 在该UIView中,我有一个子视图,我想同时在设备屏幕的高度和宽度上居中。 但是我尝试过的一切都将超级视图置于中心。 我如何才能使子视图始终位于UIDevice MainScreen的中心,而不管UIView在哪里。

我已经删除(因为其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];

添加是我的观点。 我的程序有点不寻常,因为它是用户主屏幕的音乐小部件(越狱调整),因此他们可以将其移动到任何地方。 我希望视图popArtwork始终位于屏幕的中心,无论添加在哪里。

我不知道这是否是您要查找的内容,但是当重新定位超级视图时,如何将子视图居中:

- (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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM