2016-06-13 59 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; 

}