CGPoint relative to view

2019-03-11 23:45发布

Consider a screen point (CGPoint) and a view (UIView), which is somewhere inside the view hierarchy (it can be a subview of other views).

How can you convert the point to a point relative to the view's coordinates?

1条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-03-12 00:07

First, convert the point from screen coordinates to the coordinates of your main window:

UIWindow *mainWindow = [[UIApplication sharedApplication] keyWindow];
CGPoint pointInWindowCoords = [mainWindow convertPoint:pointInScreenCoords fromWindow:nil];

Second, convert the point from window coords to view coords:

CGPoint pointInViewCoords = [myView convertPoint:pointInWindowCoords fromView:mainWindow];
查看更多
登录 后发表回答