ios 了解 convertPoint:toView:
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15109958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Understanding convertPoint:toView:
提问by Help - I need somebody's help
I do not quite understand the method convertPoint:toView:
.
我不太明白这个方法convertPoint:toView:
。
In Apple's documentation it is written that
在Apple的文档中写道
convertPoint:toView:
Converts a point from the receiver's coordinate system to that of the specified view.
- (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view
convertPoint:toView:
将一个点从接收器的坐标系转换为指定视图的坐标系。
- (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view
But what does converting a point from one to the other actually mean?
但是将一个点从一个点转换为另一个点实际上意味着什么?
Does it imply that the points in both bounds have different units? Or just different values?
这是否意味着两个边界中的点具有不同的单位?还是只是价值观不同?
If it is the latter, why is there such a method when we can simply assign the value of a's contentOffset
to b's ?
如果是后者,我们可以简单地将 a's 的值赋给contentOffset
b's ,为什么会有这样的方法?
CGPoint a = [a contentOffset];
[b setContentOffset:a];
How is convertPoint:toView:
different from simply assigning contentOffset
? Or did I misunderstand the entire concept? What does converting pointsactually do? When should this method be used?
如何convertPoint:toView:
从简单的分配不同contentOffset
?还是我误解了整个概念?什么是转换点实际上做?什么时候应该使用这种方法?
回答by John
Every UIView has its own coordinates system. So if you have a UIView_1 that contains another UIView_2, they both have a point (10,10) within them.
每个 UIView 都有自己的坐标系统。因此,如果您有一个包含另一个 UIView_2 的 UIView_1,则它们都包含一个点 (10,10)。
convertPoint:toView: allows the developer to take a point in one view and convert the point to another view coordinate system.
convertPoint:toView:允许开发人员在一个视图中取一个点并将该点转换为另一个视图坐标系。
Example: view1 contains view2. The top left corner of view2 is located at view1 point (10,10), or better to say view2.frame.orgin = {10,10}. That {10,10} is based in the view1 coordinate system. So far so good.
示例:view1 包含 view2。view2 的左上角位于 view1 点 (10,10),或者更好的说法是 view2.frame.orgin = {10,10}。{10,10} 基于 view1 坐标系。到现在为止还挺好。
The user touches the view2 at point {20,20} inside the view2. Now those coordinates are in the view2 coordinate system. You can now use covertPoint:toView: to convert {20,20} into the coordinate system of view1. touchPoint = {20,20}
用户在 view2 内部的 {20,20} 点触摸 view2。现在这些坐标在 view2 坐标系中。您现在可以使用 covertPoint:toView: 将 {20,20} 转换为 view1 的坐标系。接触点 = {20,20}
CGPoint pointInView1Coords = [view2 convertPoint:touchPoint toView:view1];
So now pointInView1Coords should be {30,30} in the view1 coordinate systems. Now that was just simple math on this example, but there are all sorts of things that contribute to the conversion. Transforms and scaling come to mind.
所以现在 pointInView1Coords 在 view1 坐标系中应该是 {30,30} 。在这个例子中,这只是简单的数学运算,但是有各种各样的东西会影响转换。转换和缩放浮现在脑海中。
Read about UIView frame, bounds, and center. They are all related and they deal with coordinate systems for a view. Its confusing until you start doing stuff with them. Remember this frame and center are in the parent's coordinate system. Bounds is in the view's coordinate system.
阅读有关 UIView 框架、边界和中心的信息。它们都是相关的,它们处理视图的坐标系。它令人困惑,直到你开始用它们做事。记住这个框架和中心在父坐标系中。边界在视图的坐标系中。
John
约翰