简体   繁体   English

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

[英]Convert a UIView origin point to its Window coordinate system

I want to get the origin of scrollView in the window coordinate system. 我想在窗口坐标系中获取scrollView的原点。 For Example, presently, scollView origin is (0,51). 例如,当前,scollView的原点是(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). 但是在窗口坐标系中,它应该是51 + 44(导航栏高度)+20(状态栏高度)=115。在窗口坐标系中,scrollView.frame.origin的值应该是(0,115)。 I tried with convertPoint: methods but getting (0,51) or (0,0) sometimes. 我尝试使用convertPoint:方法,但有时会得到(0,51)或(0,0)。 Please provide a solution to this. 请为此提供解决方案。 Thanks 谢谢

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

I tried with TechZen solution but to no avail. 我尝试使用TechZen解决方案,但无济于事。 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] . 我没有转换scrollView的原点,而是使用apple docs UIWindow类Reference,并使用此代码[self.view convertPoint:endCentre fromView:[UIApplication sharedApplication].keyWindow]将键盘的endCenter转换为视图的坐标系。 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 . 但是,问题仍然在于为什么它没有给出正确的scrollView.origin坐标。因此,基本上,我得到了一种解决方法,方法是转换键盘的中心距而不是scrollView.origin Oh, and I was doing all this stuff in order to calculate my scrollView's new height when keyboard appears on screen. 哦,我正在做所有这些事情,以便在键盘出现在屏幕上时计算scrollView的新高度。 So, if somebody has a solution to this problem or any better solution, Please let all of us know. 因此,如果有人对此问题有解决方案或更好的解决方案,请告知我们所有人。

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... 因此,如果您希望在加载视图时使用viewDidLayoutSubviews / viewDidAppear,但不能在viewDidLoad / viewWillAppear等中使用...

This code worked for me: 这段代码对我有用:

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];

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

Swift: 迅速:

In viewDidAppear(otherwise elements are not yet layered) of your ViewController: 在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)")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM