将 UIView 原点转换为其窗口坐标系

发布于 2024-09-08 14:10:59 字数 208 浏览 3 评论 0原文

我想获取scrollView在窗口坐标系中的原点。例如,目前,scollView 原点是 (0,51)。但在窗口坐标系中,它应该是 51 + 44(导航栏高度)+20(状态栏高度) = 115。这意味着在窗口坐标系中,scrollView.frame.origin 应该是 (0,115)。我尝试使用 ConvertPoint: 方法,但有时会得到 (0,51) 或 (0,0) 。请提供解决方案。谢谢

I want to get the origin of scrollView in the window coordinate system. For Example, presently, scollView origin is (0,51). But in window coordinate system it should be 51 + 44(navigation bar height)+20(status bar height) = 115. Means in window coordinate system the scrollView.frame.origin should be (0,115). I tried with convertPoint: methods but getting (0,51) or (0,0) sometimes. Please provide a solution to this. Thanks

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

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

发布评论

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

评论(4

不可一世的女人 2024-09-15 14:10:59

听起来您没有在正确的视图之间进行转换。视图的框架设置为其超级视图的坐标,而不是其自己的内部坐标,因此如果您尝试将视图的原点转换为窗口坐标,则需要使用超级视图:

[[self superview] convertPoint:self.frame.origin toView:theWindow];

但是,转换更简单从视图本身到窗口的零点。这两段代码是等效的,因此根本不需要使用 origin。

[self convertPoint:CGPointZero toView:theWindow];

It sounds like your not converting between the proper views. A view's frame is set to the coordinates of it's superview, not its own internal coordinates, so if you were trying to convert the origin of a view to window coordinates, you would need to use the superview:

[[self superview] convertPoint:self.frame.origin toView:theWindow];

However, it is even simpler to convert the zero point from the view itself to the window. The two pieces of code are equivalent, and so it isn't necessary to use the origin at all.

[self convertPoint:CGPointZero toView:theWindow];
儭儭莪哋寶赑 2024-09-15 14:10:59

我尝试使用 TechZen 解决方案但无济于事。我没有转换scrollView的原点,而是参考了苹果文档UIWindow类参考,并使用此代码将键盘的endCenter转换为我的视图的坐标系 [self.view ConvertPoint:endCentre fromView:[UIApplication sharedApplication].keyWindow]< /代码>。这段代码有效,因此解决了我的问题。 但是,问题仍然是为什么它没有给出scrollView.origin的正确坐标。所以,基本上我通过转换键盘endcenter而不是scrollView.origin得到了解决方法。哦,我正在做所有这些事情,以便在键盘出现在屏幕上时计算滚动视图的新高度。因此,如果有人有解决此问题的方法或任何更好的解决方案,请告诉我们所有人。

I tried with TechZen solution but to no avail. Instead of converting scrollView's origin,I referred to apple docs UIWindow class Reference and converted the endCenter of the keyboard to my view's coordinate system by using this code [self.view convertPoint:endCentre fromView:[UIApplication sharedApplication].keyWindow]. This piece of code worked and hence solved my problem. However, the question still remains as to why it does not gives the proper coordinate of scrollView.origin.So, basically I got a workaround by converting keyboard endcenter instead of scrollView.origin. Oh, and I was doing all this stuff in order to calculate my scrollView's new height when keyboard appears on screen. So, if somebody has a solution to this problem or any better solution, Please let all of us know.

孤独岁月 2024-09-15 14:10:59

请注意,这些功能仅在视图控制器完成子视图布局后才起作用。因此,如果您希望在视图加载时使用 viewDidLayoutSubviews / viewDidAppear,但不能在 viewDidLoad / viewWillAppear 等中使用...

这段代码对我有用:

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];

    CGPoint originInSuperview = [self.view.superview convertPoint:CGPointZero fromView:self.scrollView];
}

Note that these functions will only work after view controller finished laying out subviews. So if you want that when view loads, you can use viewDidLayoutSubviews / viewDidAppear, but not in viewDidLoad / viewWillAppear etc...

This code worked for me:

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];

    CGPoint originInSuperview = [self.view.superview convertPoint:CGPointZero fromView:self.scrollView];
}
温暖的光 2024-09-15 14:10:59

Swift:

在 ViewController 的 viewDidAppear(否则元素尚未分层)中:

        var yourFrame = yourScrollView.convertRect(yourScrollView.frame, to: self.view)

        //PRINT THE WHOLE RECT
        print(yourFrame)

        //PRINT A DETAILED VERSION OF THE FRAME, ALSO CAN PERFORM CALCULATION BETWEEN THE RECT PARAMETERS TO GET , FOR EXAMPLE, WIDTH POINT IN THE SUPERVIEW:
        print("X: \(yourFrame.origin.x) Y: \(yourFrame.origin.y) WIDTH: \(yourFrame.width) HEIGHT: \(yourFrame.height)")

Swift:

In viewDidAppear(otherwise elements are not yet layered) of your ViewController:

        var yourFrame = yourScrollView.convertRect(yourScrollView.frame, to: self.view)

        //PRINT THE WHOLE RECT
        print(yourFrame)

        //PRINT A DETAILED VERSION OF THE FRAME, ALSO CAN PERFORM CALCULATION BETWEEN THE RECT PARAMETERS TO GET , FOR EXAMPLE, WIDTH POINT IN THE SUPERVIEW:
        print("X: \(yourFrame.origin.x) Y: \(yourFrame.origin.y) WIDTH: \(yourFrame.width) HEIGHT: \(yourFrame.height)")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文