iOS 转换坐标系
2016-04-15 14:34
387 查看
iOS中视图坐标系的转换主要用到UIView的四个API:
坐标系转换的实质是: 更改坐标系的原点.
- (CGPoint)convertPoint:(CGPoint)point toView:(nullable UIView *)view;
- (CGPoint)convertPoint:(CGPoint)point fromView:(nullable UIView *)view;
- (CGRect)convertRect:(CGRect)rect toView:(nullable UIView *)view;
- (CGRect)convertRect:(CGRect)rect fromView:(nullable UIView *)view;
代码:
打印结果:
2016-04-15 15:51:33.543 坐标系转换[2744:154111] 橘色view在父控件中的frame = {{30, 30}, {150, 150}}
2016-04-15 15:51:33.544 坐标系转换[2744:154111] 橘色view在蓝色view中的frame1 = {{60, 60}, {150, 150}}
2016-04-15 15:51:33.544 坐标系转换[2744:154111] 橘色view在蓝色view中的frame2 = {{60, 60}, {150, 150}}
2016-04-15 15:51:33.544 坐标系转换[2744:154111] 橘色view在红色view中的frame = {{90, 90}, {150, 150}}
2016-04-15 15:51:33.544 坐标系转换[2744:154111] 橘色view在控制器view中的frame = {{140, 140}, {150, 150}}
2016-04-15 15:51:33.544 坐标系转换[2744:154111] 黄色view在控制view中的frame = {{110, 110}, {200, 200}}
坐标系转换的实质是: 更改坐标系的原点.
- (CGPoint)convertPoint:(CGPoint)point toView:(nullable UIView *)view;
- (CGPoint)convertPoint:(CGPoint)point fromView:(nullable UIView *)view;
- (CGRect)convertRect:(CGRect)rect toView:(nullable UIView *)view;
- (CGRect)convertRect:(CGRect)rect fromView:(nullable UIView *)view;
代码:
#import "ViewController.h" @interface ViewController () @property(nonatomic,strong)UIView *redView; @property(nonatomic,strong)UIView *blueView; @property(nonatomic,strong)UIView *yellowView; @property(nonatomic,strong)UIView *orangeView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; _redView = [[UIView alloc]initWithFrame:CGRectMake(50, 50, 300, 300)]; _redView.backgroundColor = [UIColor redColor]; [self.view addSubview:_redView]; _blueView = [[UIView alloc]initWithFrame:CGRectMake(30, 30, 250, 250)]; _blueView.backgroundColor = [UIColor blueColor]; [_redView addSubview:_blueView]; _yellowView = [[UIView alloc]initWithFrame:CGRectMake(30, 30, 200, 200)]; _yellowView.backgroundColor = [UIColor yellowColor]; [_blueView addSubview:_yellowView]; _orangeView = [[UIView alloc]initWithFrame:CGRectMake(30, 30, 150, 150)]; _orangeView.backgroundColor = [UIColor orangeColor]; [_yellowView addSubview:_orangeView]; } - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ NSLog(@"橘色view在父控件中的frame = %@",NSStringFromCGRect(self.orangeView.frame)); // 两种写法 含义相同 第二种写法 更加清晰 // 如果toView为空 默认为当前主窗口 即UIWindow CGRect orange_in_blue_frame1 = [self.orangeView.superview convertRect:self.orangeView.frame toView:self.blueView]; CGRect orange_in_blue_frame2 = [self.orangeView convertRect:self.orangeView.bounds toView:self.blueView]; NSLog(@"橘色view在蓝色view中的frame1 = %@",NSStringFromCGRect(orange_in_blue_frame1)); NSLog(@"橘色view在蓝色view中的frame2 = %@",NSStringFromCGRect(orange_in_blue_frame2)); CGRect orange_in_red_frame = [self.orangeView.superview convertRect:self.orangeView.frame toView:self.redView]; NSLog(@"橘色view在红色view中的frame = %@",NSStringFromCGRect(orange_in_red_frame)); CGRect orange_in_VCView_frame = [self.orangeView.superview convertRect:self.orangeView.frame toView:self.view]; NSLog(@"橘色view在控制器view中的frame = %@",NSStringFromCGRect(orange_in_VCView_frame)); CGRect yellow_in_VCView_frame = [self.view convertRect:self.yellowView.frame fromView:self.yellowView.superview]; NSLog(@"黄色view在控制view中的frame = %@",NSStringFromCGRect(yellow_in_VCView_frame)); } @end
打印结果:
2016-04-15 15:51:33.543 坐标系转换[2744:154111] 橘色view在父控件中的frame = {{30, 30}, {150, 150}}
2016-04-15 15:51:33.544 坐标系转换[2744:154111] 橘色view在蓝色view中的frame1 = {{60, 60}, {150, 150}}
2016-04-15 15:51:33.544 坐标系转换[2744:154111] 橘色view在蓝色view中的frame2 = {{60, 60}, {150, 150}}
2016-04-15 15:51:33.544 坐标系转换[2744:154111] 橘色view在红色view中的frame = {{90, 90}, {150, 150}}
2016-04-15 15:51:33.544 坐标系转换[2744:154111] 橘色view在控制器view中的frame = {{140, 140}, {150, 150}}
2016-04-15 15:51:33.544 坐标系转换[2744:154111] 黄色view在控制view中的frame = {{110, 110}, {200, 200}}
相关文章推荐
- IOS学习之——界面2传值到界面1(代理方法)
- iOS开发系列--数据存取
- ios 之控制器的loadview
- iOS 上传照片到服务器 获取相册和拍照功能的照片 照片压缩上传
- GCD的一些原理
- iOS 获取系统图片
- iOS-上拉刷新,下拉加载-----------详解
- iOS崩溃crash大解析
- iOS 在tableView或scrollView上,消退键盘
- iOS开发之WMPageController的使用
- iOS RunLoop笔记
- iOS开发 引入支付宝 缺少 #include <openssl/asn1.h>
- iOS 版本更新迭代
- iOS开发中的一些图片上传压缩处理
- iOS 相关路径
- iOS常用数学常量宏
- iOS常用的数学函数
- 【iOS学习笔记 16-4-15】正则表达式基础
- ios / % 四舍五入 向上取整(ceil()) 向下取整(floor())
- Masonry