UIView绝对屏幕点
有没有一种简单的方法来获得UIView的绝对点? 绝对的意思是在iPad上的UIView相对于1024×768的位置?
我需要一个简单的方法,因为我有一个UITableViewCell内的UITableViewCell里面的一个视图控制器,是一个重复到第五或第六个视图控制器的另一个视图控制器的孩子提出的视图内的UIButton。 而由于这种复杂的devise,我需要UIButton的绝对矩形,所以我可以在“主”视图控制器上显示UIActionSheet。
所以,我们有一个叫它的小视图,里面有一个特定的点。 这个点相对于其父视图的本地坐标是小视图。 现在,我们想要知道,如果它保持在屏幕上的同一个点上,那么这个点的坐标是什么 ,但它将是根视图的一部分。 (这实际上给了我们相对于整个屏幕的坐标。)
您将计算该点的全局坐标
CGPoint globalCoordinates = [self.smallView convertPoint:localCoordinatesOfThePoint toView:self.view];
localCoordinatesOfThePoint
也是一个CGPoint结构。
另一种情况
是当你有整个屏幕覆盖的根视图,这有一个很大的看法,(仍然小于根视图虽然),这个大视图有一个小视图作为子视图里面。
您将计算小视图的提示的位置
CGPoint originOnScreen = [self.smallView convertPoint:self.smallview.origin toView:self.view];
self.smallview.origin
是一个相对于Large视图的CGPoint。 因此,我们计算,如果这个点(它实际上是小视图的左上angular)将在根视图内,它的坐标是多less?
抓住
如果根视图不是全屏,而不是根视图,我们需要获得对主窗口的引用,因为这将成为我们现在计算坐标的视图。
我们必须
#import "AppDelegate.h"
在我们计算的类中,然后传递窗口属性作为参考视图。 确保窗口覆盖屏幕,UIWindow是UIView的子类。 所以这只是工作。
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; CGPoint originOnScreen = [self.smallView convertPoint:self.smallview.origin toView:appDelegate.window];
你在找什么是:
- (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view
这将把一个点从接收者的坐标系(你的button)转换为指定视图(顶层视图)的点。
文档
如果你想获得某个UIView
CGPoint
/ CGRect
的坐标,可以使用UIView
方法
- (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view
或CGRect
- (CGRect)convertRect:(CGRect)rect toView:(UIView *)view
在第一个参数中放置要转换的CGPoint
/ CGRect
,然后在第二个放置要转换的UIView
所以我最终使用UITapGestureRecognizer来代替计算位置,如下所示:
CGPoint gestureViewLocation = [gesture locationInView:gesture.view]; CGPoint absoluteLocation = [gesture locationInView:self.parentViewController.view]; CGRect fromRect = CGRectMake(absoluteLocation.x-gestureViewLocation.x, //subtract to we get point relative to gesture view absoluteLocation.y-gestureViewLocation.y, //subtract to we get point relative to gesture view gesture.view.frame.size.width, gesture.view.frame.size.height);
要从视图的本地框架或path转换为SCREEN坐标,请使用内置的转换器:
UIAccessibilityConvertPathToScreenCoordinates
要么
UIAccessibilityConvertFrameToScreenCoordinates
这确实会返回一个path或矩形,但是您可以从中提取点。
斯威夫特4转换,似乎适用于两个以上的子视图级别:
// given that myView is in hierarchy of rootView, either // have rootView convert its origin from myView, or // have myView convert rootView's origin to rootView rootView.convert(rootView.origin, from: myView) myView.convert(rootView.origin, to: rootView)
我发现转换程序有点混乱。 所以,我蛮力testing了所有可能的组合5级的意见。 结果发布在github上