2016-06-13 29 views
0

我有一个可以拖动的uview。在那个UIView中,我有一个子视图,我想在设备屏幕的高度和宽度上居中。但是,我试过的东西是以超视图为中心的。我怎么能让子视图总是在UIDevice MainScreen的中心,尽管UIView在哪里。尽管Superview的位置,但在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始终处于屏幕的中心位置,无论添加位置如何。

+0

向我们展示你做了什么至今 –

+0

我不请记住UIView的原点是0,0还是0.5,0.5 。但它应该是这样的: ** CGPoint localCenter = [self.view.superview convertPoint:CGPointZero fromView:[[[UIApplication sharedApplication] delegate ] window]]; ** –

+0

@UmairAfzal我加了我认为有必要的东西。 – TheHellOTrofasdasd

回答

0

我不知道这是否是你所寻找的,但它如何中心子视图时,上海华盈是重新定位:

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

}