美文网首页
坐标系的转换

坐标系的转换

作者: 冷武橘 | 来源:发表于2020-04-15 16:21 被阅读0次
0CE02AD6-5C7C-4A42-B672-D6B85E492AC4.png

首先用代码创建如图所示的图层:
UIView *redview=[[UIView alloc]initWithFrame:CGRectMake(100, 100, 200, 200)];
redview.backgroundColor=[UIColor redColor];
[self.view addSubview:redview];
UIView *blueview=[[UIView alloc]initWithFrame:CGRectMake(50, 50, 100, 100)];
blueview.backgroundColor=[UIColor blueColor];
[redview addSubview:blueview];
如上图的红色View,以控制器的view为父控件作为坐标系原点,那么它的frame的x = 100,y = 100;而蓝色view是以红色view作为坐标系原点,那么他的frame x=50,y=50;有时为了比较两个view是否重叠,或者相对于主窗口获取位置等就需要将两者坐标系转换成同一个坐标系。主要通过以下两个方法进行转换:

 //rect用来确定矩形框原来位置
 (CGRect)convertRect:(CGRect)rect toView:(nullable UIView *)view;
 (CGRect)convertRect:(CGRect)rect fromView:(nullable UIView *)view;

  //让rect这个矩形框,从view2坐标系转换到view1坐标系,得出一个新的矩形框newrect
 [view1 converRect:rect fromview:view2]

//让rect这个矩形框,从view1坐标系转换到view2坐标系,得出一个新的矩形框newrect
 [view1 convertRect:rect toView:view2]
 //确定blueview在self.view的frame的四种方式:
    CGRect newrect= [self.view convertRect:blueview.frame fromView:redview];
    NSLog(@"%@",NSStringFromCGRect(newrect));

    CGRect newrect= [self.view convertRect:blueview.bounds fromView:blueview];
    NSLog(@"%@",NSStringFromCGRect(newrect));

    CGRect newrect= [redview convertRect:blueview.frame toView:self.view];
    NSLog(@"%@",NSStringFromCGRect(newrect));

    CGRect newrect= [blueview convertRect:blueview.bounds toView:self.view];
    NSLog(@"%@",NSStringFromCGRect(newrect));

将控件的位置转换到主窗口:

//传nil后进去会默认转换到主窗口
[blueview convertRect:blueview.bounds toView:nil];

//常用
[blueview convertRect:blueview.bounds toView:[UIApplication sharedApplication].keyWindow];

[[UIApplication sharedApplication].keyWindow convertRect:blueview.bounds fromView:blueview];

[blueview convertRect:blueview.superview.frame toView:[UIApplication sharedApplication].keyWindow];

[blueview convertRect:redview.frame toView:[UIApplication sharedApplication].keyWindow];

 [[UIApplication sharedApplication].keyWindow convertRect:redview.frame fromView:blueview];

相关文章

  • 2018-03-04

    常用坐标系统知识点 1.坐标系统之间的转换 (1)坐标系分类 不同参心坐标系之间的转换、不同地心坐标系之间的转换;...

  • 地图坐标转换

    地图坐标转换 简介 各地图API坐标系统比较与转换; WGS84坐标系:即地球坐标系,国际上通用的坐标系。设备一般...

  • 高等数学预备知识

    极坐标 极坐标系是由极轴、极径组成极坐标系上的点表示为(ρ,θ)极坐标系上的点转换成直角坐标直角坐标系上的点转换成...

  • week51 坐标变换与坐标系变换

    坐标转换是一个坐标在不同坐标系下的表示,而坐标系转换不同坐标系的相对位姿关系。 TF介绍TF(TransForm)...

  • 地理坐标系转换 API 接口

    地理坐标系转换 API 接口 提供地理信息坐标系的相互转换。 1. 产品功能 支持多种地理信息坐标系; 高精度坐标...

  • Qt 绘图转换

    转换 QTransform 用于指定坐标系的 2D 转换 - 平移、缩放、扭曲(剪切)、旋转或投影坐标系。绘制图形...

  • 环境

    1. 导入需要的库 2. 定义三角函数用于坐标系转换 3. 定义转换矩阵:大地坐标系---->随体坐标系 4. 定...

  • 顶视图-世界坐标系-相机坐标系-Scara坐标系转换

    顶视图-世界坐标系-相机坐标系-Scara坐标系转换 1. 定义 Scara相机坐标系 标准相机坐标系 顶视图坐标...

  • 矢量图形坐标转换-wgs84与GCJ02互转

    借助WGIS,我们可以将WGS84坐标系下的任意图形转换为GCJ02坐标系,或者将GCJ02坐标系的图形转换为WG...

  • 超简单的canvas绘制地图

        本文使用geojson数据,通过缩放和平移把地图的地理坐标系转换canvas的屏幕坐标系,然后将转换后的数...

网友评论

      本文标题:坐标系的转换

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