CGPoint相对于视图 [英] CGPoint relative to view

查看:194
本文介绍了CGPoint相对于视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一个屏幕点(CGPoint)和一个视图(UIView),它位于视图层次结构中的某个位置(它可以是其他视图的子视图)。

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?

推荐答案

首先,将点从屏幕坐标转换为主窗口的坐标:

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

这篇关于CGPoint相对于视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆