获取 Objective C iPad 应用程序中任何控件的绝对位置

发布于 2024-11-15 15:49:57 字数 221 浏览 2 评论 0原文

我想知道是否有办法可以获取 ipad/iphone 应用程序中控件的绝对位置。例如,我有一个 TextField,它位于视图子视图的子视图中。我想知道与顶部父视图相关的 X 和 Y 值(例如,当前文本字段的 x 和 Y 返回 10 和 10,因为这是它在其自己的视图中的框架位置,但我想知道这与它的父级应该类似于 X = 10 和 Y = 220)。我需要为此制定一个通用方法。希望这是有道理的。

有什么想法吗?

Im wondering if there is anyway that you can get the absolute location of a control in a ipad/iphone application. e.g. I have a TextField which is within a child of a child of a view. I want to know the X and Y values in relation to the top Parent View (e.g. currently the x and Y of the textfield return 10 and 10 because that is it's frame location within its own view, but I want to know this in relation to its parent which should be something like X = 10 and Y = 220). I need to make a generic method for this somehow. Hope this make sense.

Any ideas?

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

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

发布评论

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

评论(1

提笔书几行 2024-11-22 15:49:57

您正在寻找 -[UIView ConvertPoint:toView:]

例如,要根据窗口的基坐标获取视图 view 的原点,您可以这样写:

CGPoint localPoint = [view bounds].origin;
CGPoint basePoint = [view convertPoint:localPoint toView:nil];

如果您想将该点转换为同一窗口内其他视图的坐标系,您可以使用该视图作为 toView: 参数而不是 nil

NSAssert([view window] == [otherView window],
        @"%s: Views must be part of the same window.", __func__);
CGPoint otherPoint = [view convertPoint:localPoint toView:otherView];

因为不同的坐标系可以有不同的比例,您可能会发现 -convertRect:toView:< /code> 更有用,具体取决于什么你打算处理坐标。还有类似的 -fromView: 版本的点和矩形转换方法。

You are looking for -[UIView convertPoint:toView:].

For example, to get the origin of a view view in terms of its window's base coordinates, you would write:

CGPoint localPoint = [view bounds].origin;
CGPoint basePoint = [view convertPoint:localPoint toView:nil];

If you instead want to convert the point into the coordinate system of some other view within the same window, you can use that view as the toView: argument instead of nil:

NSAssert([view window] == [otherView window],
        @"%s: Views must be part of the same window.", __func__);
CGPoint otherPoint = [view convertPoint:localPoint toView:otherView];

Because different coordinate systems can have different scales, you might find -convertRect:toView: to be more useful, depending on what you're planning to do with the coordinates. There are also analogous -fromView: versions of both the point and rect conversion methods.

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