美文网首页
iOS转换坐标

iOS转换坐标

作者: AprSnow | 来源:发表于2018-03-20 14:35 被阅读11次

方法

ios转换坐标有两个方法:convertRect:fromView:convertRect:toView:,本文介绍这两个方法的用法。

首先考虑如下代码:

UIView *viewA = [[UIView alloc] initWithFrame:
                            CGRectMake(0, 100, self.view.frame.size.width, 400)];
UIView *viewB = [[UIView alloc] initWithFrame:
                            CGRectMake(0, 50, 150, 150)];

[viewA addSubview:viewB];
[self.view addSubview:viewA];

CGRect rect1 = [self.view convertRect:viewB.frame fromView:viewA];
CGRect rect2 = [self.view convertRect:viewB.frame toView:viewA];

rect1rect2 分别是什么位置?

ViewA and ViewB

convertRect:fromView:

Converts a rectangle from the coordinate system of another view to that of the receiver.

把一个矩形从另一个视图的坐标系转换到接受者的坐标系。

CGRect rect1 = [self.view convertRect:viewB.frame fromView:viewA];
// rect = (origin = (x = 0, y = 150), size = (width = 150, height = 150))

从代码中可以看出,rect1 表示 viewA 中的 viewBself.view 坐标系中的位置。

convertRect:toView:

Converts a rectangle from the receiver’s coordinate system to that of another view.

把一个矩形从接受者的坐标系转到另一个视图的坐标系。

CGRect rect2 = [self.view convertRect:viewB.frame toView:viewA];
// rect = (origin = (x = 0, y = -50), size = (width = 150, height = 150))

从代码中可以看出,rect2 表示在self.view坐标系中,viewB( frame = (0, 50, 150, 150) )相对于viewA( frame = (0, 100, width, 400) )的位置。

相关文章

  • iOS转换坐标

    方法 ios转换坐标有两个方法:convertRect:fromView: 和 convertRect:toVie...

  • iOS坐标转换

  • iOS 坐标转换

    说明:同一屏幕上的视频,可以进行坐标转换。 UIView方法1 ------------------------...

  • IOS坐标转换

    //将像素point由point所在视图转换到目标视图view中,返回在目标视图view中的像素值 - (CGPo...

  • [iOS]坐标转换

    JZLocationConverter.h JZLocationConverter.m

  • ios 坐标转换

    // 将像素point由point所在视图转换到目标视图view中,返回在目标视图view中的像素值 - (CGP...

  • iOS 坐标转换

    首先添加一个redView和一个greenView,其中greenView是添加到redView上面。 UIVie...

  • iOS坐标转换

    转换方法声明 @interface UIView(UIViewGeometry) 案例 controllerA 中...

  • iOS坐标转换

    //将像素point由point所在视图转换到目标视图view中,返回在目标视图view中的像素值 - (CGPo...

  • ios 地图坐标系转换

    ios 地图坐标系转换 https://segmentfault.com/a/1190000003023989 摘自网页

网友评论

      本文标题:iOS转换坐标

      本文链接:https://www.haomeiwen.com/subject/vsovqftx.html