如何获取UINavigationController中使用的UIView原点的x / y坐标?

时间:2011-09-14 23:01:57

标签: iphone ios uiview uinavigationcontroller touchesmoved

如何获取UINavigationController中使用的UIView原点的x / y坐标?

那就是我有一个UINavigationController iPhone应用程序设置。我将一个新视图(称为CustomUIView)推入堆栈。如何才能将CustomView视图的绝对原点(x / y cooridnate)与整个iPhone屏幕相关联?也就是说,它不应该是0,0因为它应该考虑顶部的导航栏等。

我注意到我得到: *对于NavController:frame =(0 0; 320 480) *对于customViewController.view:frame =(0 0; 320 416) *对于customView本身:frame =(0 0; 320 396)

所以就像麻烦一样,当我在customView代码本身,并且我得到它的框架时,它给我0,0的原点...

背景:我正在尝试将图像定位在customView上的某个位置,但它似乎是偏移的,我猜是因为当我试图设置它相对于customView的位置时,也许我应该相对于NavController整体框架设置它。我希望图像居中的位置基于在屏幕上获取触摸事件:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {  
    for (UITouch *touch in touches){
        [self dispatchTouchEvent:[touch locationInView:self]];
    }
}

PS。也许另一个有用的答案是,如果您确实从UITouch事件获得坐标,那么想要使用它来将图像置于这些坐标中心,您将在基于UINavigationController的iPhone中的UIview子视图中使用的代码是什么应用程序,可以正确地使图像居中....

1 个答案:

答案 0 :(得分:2)

CGPoint convertedPoint = [self.view.superview convertPoint:self.view.frame.origin toView:nil];

请参阅convertPoint文档:toView: http://developer.apple.com/library/ios/DOCUMENTATION/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/instm/UIView/convertPoint:toView