了解 convertRect:toView:、convertRect:FromView:、convertPoint:toView: 和 convertPoint:fromView: 方法 [英] Understand convertRect:toView:, convertRect:FromView:, convertPoint:toView: and convertPoint:fromView: methods

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

问题描述

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

I'm trying to understand the functionalities of these methods. Could you provide me with a simple use case to understand their 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.

坐标系是什么意思?接收器怎么样?

例如,像下面这样使用 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, 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 以超视图 (self.window) 坐标表示.输出如下:

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

convertPoint:toView: {0, 0}

convertPoint:toView: {0, 0}

推荐答案

每个视图都有自己的坐标系——原点在 0,0,宽度和高度.这在视图的 bounds 矩形中描述.然而,视图的 frame 的原点将位于其父视图边界矩形内的点.

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: 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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