How to convert window coordinates relative to a specific view?

自作多情 提交于 2019-12-22 07:08:38

问题


Example: I have a CGPoint in window coordinates:

CGPoint windowPoint = CGPointMake(220.0f, 400.0f);

There is aView which has a superview in a superview in a superview. Somewhere deep in the view hierarchy, probably even transformed a few times.

When you get a UITouch, you can ask it for -locationInView: and it will return the coordinates relative to that view.

I need pretty much the same thing. Is there an easy way to accomplish that?


回答1:


I found a really easy solution:

[self.aView convertPoint:windowPoint fromView:self.window];



回答2:


Perhaps you could iterate through the superview tree, drilling down to subviews by tag, and add or subtract the subview's frame.origin values to get to a translated windowPoint relative to the view-of-interest's frame.origin.



来源:https://stackoverflow.com/questions/2906787/how-to-convert-window-coordinates-relative-to-a-specific-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!