理解convertRect:toView:,convertRect:FromView:,convertPoint:toView:和convertPoint:fromView:methods [英] Understand convertRect:toView:, convertRect:FromView:, convertPoint:toView: and convertPoint:fromView: methods

查看:608
本文介绍了理解convertRect:toView:,convertRect:FromView:,convertPoint:toView:和convertPoint:fromView:methods的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试了解这些方法的功能。你能给我一个简单的用例来理解他们的语义吗?

I'm trying to understand the functionalities of these methods. Could you provide me a simple usecase to understand theirs semantics?

从文档中,例如, convertPoint:fromView:方法描述如下:

From the documentation, for example, convertPoint:fromView: method is described as follows:


将一个点从给定视图的坐标系转换为接收者的坐标系。

Converts a point from the coordinate system of a given view to that of the receiver.

坐标系是什么意思?那么接收器呢?

What does the coordinate system mean? What about the receiver?

例如,使用 convertPoint:fromView:,如下所示是否有意义?

For example, does it make sense using convertPoint:fromView: like the following?

CGPoint p = [view1 convertPoint:view1.center fromView:view1];

使用NSLog实用程序,我已经验证了p值与view1的中心重合。

Using NSLog utility, I've verified that p value coincides with view1's center.

提前感谢。

编辑:对于有兴趣的人,我创建了一个简单的代码片段来理解这些方法。

for those interested in, I've created a simple code snippet to understand these methods.

UIView* view1 = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 150, 200)];
view1.backgroundColor = [UIColor redColor];

NSLog(@"view1 frame: %@", NSStringFromCGRect(view1.frame));        
NSLog(@"view1 center: %@", NSStringFromCGPoint(view1.center));   

CGPoint originInWindowCoordinates = [self.window convertPoint:view1.bounds.origin fromView:view1];        
NSLog(@"convertPoint:fromView: %@", NSStringFromCGPoint(originInWindowCoordinates));

CGPoint originInView1Coordinates = [self.window convertPoint:view1.frame.origin toView:view1];        
NSLog(@"convertPoint:toView: %@", NSStringFromCGPoint(originInView1Coordinates));

在这两种情况下,self.window都是接收者。但是有区别。在第一种情况下,convertPoint参数以view1坐标表示。输出如下:

In both cases self.window is the receiver. But there is a difference. In the first case the convertPoint parameter is expressed in view1 coordinates. The output is the following:


convertPoint:fromView:{100,100}

convertPoint:fromView: {100, 100}

在第二个中,convertPoint以superview(self.window)坐标表示。输出如下:

In the second one, instead, the convertPoint is expressed in superview (self.window) coordinates. The output is the following:


convertPoint:toView:{0,0}

convertPoint:toView: {0, 0}


推荐答案

每个视图都有自己的坐标系 - 原点为0,0,宽度和高度。这在视图的 bounds 矩形中描述。但是,视图的框架的起源将位于其超视图的边界矩形内。

Each view has its own coordinate system - with an origin at 0,0 and a width and height. This is described in the bounds rectangle of the view. The frame of the view, however, will have its origin at the point within the bounds rectangle of its superview.

视图层次结构的最外层视图的原点是0,0,它对应于iOS中屏幕的左上角。

The outermost view of your view hierarchy has it's origin at 0,0 which corresponds to the top left of the screen in iOS.

如果在该视图中添加20,30的子视图,则子视图中的0,0点对应于超视图中的20,30点。这种转换就是那些方法正在做的。

If you add a subview at 20,30 to this view, then a point at 0,0 in the subview corresponds to a point at 20,30 in the superview. This conversion is what those methods are doing.

上面的例子毫无意义(没有双关语),因为它将一个点从视图转换为自身,所以什么都不会发生。您可以更常见地查看某个视图与其超级视图相关的位置 - 测试视图是否在屏幕上移动,例如:

Your example above is pointless (no pun intended) since it converts a point from a view to itself, so nothing will happen. You would more commonly find out where some point of a view was in relation to its superview - to test if a view was moving off the screen, for example:

CGPoint originInSuperview = [superview convertPoint:CGPointZero fromView:subview];

接收者是接收消息的对象的标准objective-c术语(方法也被称为消息)所以在我的例子中接收器是 superview

The "receiver" is a standard objective-c term for the object that is receiving the message (methods are also known as messages) so in my example here the receiver is superview.

这篇关于理解convertRect:toView:,convertRect:FromView:,convertPoint:toView:和convertPoint:fromView:methods的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆