ios开发之坐标系转换
1:坐标系转换最核心的问题就是:比较两个坐标是否包含,或者是重叠等,最主要的问题是先将两个坐标转换到同一个坐标系下再去比较。第一步先确定矩形框在某个view坐标系下的frame(该矩形框是以该view的左上角为坐标原点)2:再转换到另一个view坐标系下(转换后的坐标依然是以另一个view的坐标原点来计算得出新坐标系下的矩形框)
2:坐标系的转化方法:1:CGRectContainsRect(<#CGRect rect1#>, <#CGRect rect2#>),判断rect1矩形框是否包含rect2矩形框 2:CGRectIntersectsRect(<#CGRect rect1#>, <#CGRect rect2#>):判断rect1的矩形框是否和rect2的矩形框重叠 3:
CGRectContainsPoint(<#CGRect rect#>, <#CGPoint point#>) ,判断某个点是否包含在矩形框内,4:以上三个方法的使用前提是都要在同一个坐标系下来判断,若不是在同一个坐标系下,利用坐标系转换转到同一个坐标系下,在进行比较。
#import "ViewController.h" @interface ViewController ()
/** 蓝色 */
@property (nonatomic, weak) UIView *blueView;
/** 橙色 */
@property (nonatomic, weak) UIView *orangeView;
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; UIView *blueView = [[UIView alloc] init];
blueView.backgroundColor = [UIColor blueColor];
blueView.frame = CGRectMake(, , , );
blueView.alpha = 0.5;
[self.view addSubview:blueView];
self.blueView = blueView; UIView *orangeView = [[UIView alloc] init];
orangeView.backgroundColor = [UIColor orangeColor];
orangeView.frame = CGRectMake(, , , );
orangeView.alpha = 0.5;
[self.view addSubview:orangeView];
self.orangeView = orangeView; // CGRectContainsRect(<#CGRect rect1#>, <#CGRect rect2#>)
// CGRectIntersectsRect(<#CGRect rect1#>, <#CGRect rect2#>)
//CGRectContainsPoint(<#CGRect rect#>, <#CGPoint point#>)
} - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
// self.blueView.bounds = {0, 0, 100, 100}
// self.orangeView.bounds = {0, 0, 100, 100} // self.blueView.frame = {50, 50, 100, 100}
// self.orangeView.frame = {200, 200, 100, 100} NSLog(@"%zd", CGRectIntersectsRect(self.blueView.frame, self.orangeView.frame));
} @end
总结:1:上部分代码两个view是在同一个坐标系下,都是以屏幕左上角为坐标原点,所以调用方法可以判断两个view是否是包含关系或是重叠关系 2:注意点:在touchBeagn方法的打印中,判断两个view是否是重叠关系,传入两个view的rect值,注意此时的rect值不能传入bounds,只能传入frame,因为两者需要在同一个坐标系下作比较,两者的父控件都是self.view,所以两者的frame都是以父控件的坐标原点计算的frame,而bounds确是以自身控件的原点计算的frame。所以用bounds两者根本不在同一个坐标下,所以只能传入frame
3:坐标系的相互转换:不同坐标系转换到同一个坐标系下,进行比较:其中最核心的关键点是:1:要将不同坐标下的矩形框转换到统一坐标系下进行比较 2:第一步先要确定出矩形框在原坐标系下的frame,只有确定了矩形框在原来坐标系下的位置,才能准确转换到另一个坐标系中得到新的rect值 3:可以调用方法from,或是to方法进行坐标系的转换[view1 convertRect:rect fromView:view2];[view1 convertRect:rect toView:view2];其中无论是新转换的rect还是待转换的rect,都是以view1或是view2的左上角为坐标原点计算的
view2坐标系 : 以view2的左上角为坐标原点
view1坐标系 : 以view1的左上角为坐标原点
CGRect newRect = [view1 convertRect:rect fromView:view2];
让rect这个矩形框, 从view2坐标系转换到view1坐标系, 得出一个新的矩形框newRect
rect和view2的含义 : 用来确定矩形框原来在哪
CGRect newRect = [view1 convertRect:rect toView:view2];
让rect这个矩形框, 从view1坐标系转换到view2坐标系, 得出一个新的矩形框newRect
rect和view1的含义 :用来确定矩形框原来在哪
#import "ViewController3.h" @interface ViewController3 ()
@property (weak, nonatomic) IBOutlet UIView *blueView;
@property (weak, nonatomic) IBOutlet UIView *redView;
@end @implementation ViewController3 - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
} - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
// view2坐标系 : 以view2的左上角为坐标原点
// view1坐标系 : 以view1的左上角为坐标原点
//
// CGRect newRect = [view1 convertRect:rect fromView:view2];
// 让rect这个矩形框, 从view2坐标系转换到view1坐标系, 得出一个新的矩形框newRect
// rect和view2的含义 : 用来确定矩形框原来在哪
//
// CGRect newRect = [view1 convertRect:rect toView:view2];
// 让rect这个矩形框, 从view1坐标系转换到view2坐标系, 得出一个新的矩形框newRect
// rect和view1的含义 :用来确定矩形框原来在哪 // 确定redView在window中的位置和尺寸
// CGRect newRect = [self.redView convertRect:self.redView.bounds toView:[UIApplication sharedApplication].keyWindow];
// CGRect newRect = [self.redView.superview convertRect:self.redView.frame toView:[UIApplication sharedApplication].keyWindow];
CGRect newRect = [self.redView convertRect:self.redView.bounds toView:nil];
// CGRect newRect = [self.redView.superview convertRect:self.redView.frame toView:nil];
// CGRect newRect = [[UIApplication sharedApplication].keyWindow convertRect:self.redView.bounds fromView:self.redView];
// CGRect newRect = [[UIApplication sharedApplication].keyWindow convertRect:self.redView.frame fromView:self.redView.superview];
NSLog(@"%@", NSStringFromCGRect(newRect));
} @end
总结:1:坐标系的换换用to方法:1:当利用此方法做坐标系的转换时,toView的参数传空值nil的时候,默认为当前的主窗口。2:当做不同坐标系的相互转换的时候,利用bounds和frame都可以,主要是通过bounds或是frame来确定待矩形框在原view的位置,还要是确定了位置就可以实现不同坐标系下的转换。bounds是以自身view为坐标原点来确定矩形框的位置,传入的参数为自身的view和bounds,而frame是以其父控件的左上角为坐标原点,传入的参数为父控件的view和矩形框的frame。3:要想拿到某个控件的父控件,就是superView,self.redView.superview
// 确定redView在window中的位置和尺寸
CGRect newRect = [self.redView convertRect:self.redView.bounds toView:[UIApplication sharedApplication].keyWindow];
CGRect newRect = [self.redView convertRect:self.redView.bounds toView:nil];
CGRect newRect = [self.redView.superview convertRect:self.redView.frame toView:[UIApplication sharedApplication].keyWindow];
CGRect newRect = [self.redView.superview convertRect:self.redView.frame toView:nil];
4:判断在不同坐标下的两个矩形框是否包含关系或者是否是重叠关系:1:首先将两个处在不同坐标系下的矩形框转换到同一个坐标系在 2:在调用包含,重叠的方法,会返回一个Bool类型的返回值,来判断两个矩形框的关系
#import "ViewController2.h" @interface ViewController2 ()
@property (weak, nonatomic) IBOutlet UIView *greenView;
@property (weak, nonatomic) IBOutlet UIView *whiteView;
@end @implementation ViewController2 - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
} - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
// CGPoint point = [self.whiteView convertPoint:CGPointMake(50, 50) fromView:self.greenView];
// NSLog(@"%@", NSStringFromCGPoint(point)); CGRect whiteRect = [self.whiteView convertRect:self.whiteView.bounds toView:nil];
CGRect greenRect = [self.greenView convertRect:self.greenView.bounds toView:nil]; NSLog(@"greenView - %@", NSStringFromCGRect(greenRect));
NSLog(@"whiteView - %@", NSStringFromCGRect(whiteRect));
NSLog(@"%zd", CGRectIntersectsRect(greenRect, whiteRect)); } - (void)test
{ NSLog(@"greenView - %@", NSStringFromCGRect(self.greenView.frame));
NSLog(@"whiteView - %@", NSStringFromCGRect(self.whiteView.frame)); // greenView - {{10, 10}, {80, 80}} // whiteView - {{150, 150}, {80, 80}} NSLog(@"%zd", CGRectIntersectsRect(self.greenView.frame, self.whiteView.frame));
} @end
ios开发之坐标系转换的更多相关文章
- iOS开发进制转换
1.十进制转换为二进制 /** 十进制转换为二进制 @param decimal 十进制数 @return 二进制数 */ + (NSString *)getBinaryByDecimal:(NSIn ...
- [转]iOS开发中的火星坐标系及各种坐标系转换算法
iOS开发中的火星坐标系及各种坐标系转换算法 源:https://my.oschina.net/u/2607703/blog/619183 其原理是这样的:保密局开发了一个系统,能将实际的坐标转 ...
- iOS开发中的火星坐标系及各种坐标系转换算法
原文地址:http://m.oschina.net/blog/619183?ref=myread 其原理是这样的:保密局开发了一个系统,能将实际的坐标转换成虚拟的坐标.所有在中国销售的数字地图必须使用 ...
- iOS开发时间戳与时间NSDate,时区的转换,汉字与UTF8,16进制的转换
http://blog.sina.com.cn/s/blog_68661bd80101njdo.html 标签: ios时间戳 ios开发时间戳 ios16进制转中文 ios开发utf8转中文 ios ...
- (原)Android到IOS开发的转换(一)
序)闲扯几句 很早就想入手ios开发,但是一直没有机会,个人没有水果机器,上个公司上班的那台mac mini虽然就在我身边,灰都有一层了,但是一直没有机会开机学习下,因为事多,自上一篇文章后,离职后, ...
- NX二次开发-UFUN CSYS坐标系转换UF_CSYS_map_point
1 NX9+VS2012 2 3 #include <uf.h> 4 #include <uf_curve.h> 5 #include <uf_csys.h> 6 ...
- iOS开发系列--打造自己的“美图秀秀”
--绘图与滤镜全面解析 概述 在iOS中可以很容易的开发出绚丽的界面效果,一方面得益于成功系统的设计,另一方面得益于它强大的开发框架.今天我们将围绕iOS中两大图形.图像绘图框架进行介绍:Quartz ...
- 【IOS开发笔记03-视图相关】简单计算器的实现
UIView 经过前几天的快速学习,我们初步了解的IOS开发的一些知识,中间因为拉的太急,忽略了很多基础知识点,这些知识点单独拿出来学习太过枯燥,我们在今后的项目中再逐步补齐,今天我们来学习APP视图 ...
- iOS开发之微信聊天页面实现
在上篇博客(iOS开发之微信聊天工具栏的封装)中对微信聊天页面下方的工具栏进行了封装,本篇博客中就使用之前封装的工具栏来进行聊天页面的编写.在聊天页面中主要用到了TableView的知识,还有如何在俩 ...
随机推荐
- monkey基础知识(二)
- java 位操作 bitwise(按位) operation bit
java 位操作 bitwise(按位) operation bit //一篇对于 原码 反码 补码 的介绍 http://www.cnblogs.com/zhangziqiu/archive/201 ...
- 三、Docker镜像的相关操作
原文:三.Docker镜像的相关操作 一.查看本地镜像: docker images 二.使用某个镜像来运行容器: docker run -t -i xxxx(镜像名):xx.xx(版本,不带即最新) ...
- CISP/CISA 每日一题 12
CISA 每日一题(答) 支付系统模式有哪些: 电子现金模式:支付者不必在线,无条件不可追溯性 电子支票模式:支付者不必在线,涉及个人隐私 电子转帐模式:收款人不必在线 图象处理中,应该有适当的___ ...
- Vue 学习记录<1>
1.环境搭建:(前提node.js搭建) # 全局安装 vue-cli $ npm install --global vue-cli # 创建一个基于 webpack 模板的新项目 $ vue i ...
- [Vue + TS] Using Route events inside Vue
vue-router introduces new hooks into the component. In this lesson we’ll show you how to use these n ...
- Android 6.0 最简单的权限获取方法 RxPermition EasyPermition
Android 6.0 要单独的获取权限 这里提供两种很简单的方法 EasyPermition RxPermition EasyPermition https://github.com/googles ...
- Python编写Appium测试用例(1)
有段时间没有使用python编写测试用例了,很长时间以来,感觉appium这个测试工具确实不错,今天又重新拿起来,分享一下自己学习的一些用例,欢迎大家一起交流.学习! 1.登录客户端 #coding= ...
- MyBatis学习总结(13)——Mybatis查询之resultMap和resultType区别
MyBatis的每一个查询映射的返回类型都是ResultMap,只是当我们提供的返回类型属性是resultType的时候,MyBatis对自动的给我们把对应的值赋给resultType所指定对象的属性 ...
- [寒江孤叶丶的Cocos2d-x之旅_36]用LUA实现UTF8的字符串基本操作 UTF8字符串长度,UTF8字符串剪裁等
原创文章,欢迎转载,转载请注明:文章来自[寒江孤叶丶的Cocos2d-x之旅系列] 博客地址:http://blog.csdn.net/qq446569365 一个用于UTF8字符串操作的类.功能比較 ...