相对于视图的CGPoint

发布于 2024-10-06 04:28:53 字数 94 浏览 0 评论 0原文

考虑一个屏幕点(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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

毁虫ゝ 2024-10-13 04:28:53

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

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

其次,将点从窗口坐标转换为视图坐标:

CGPoint pointInViewCoords = [myView convertPoint:pointInWindowCoords fromView:mainWindow];

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文