UIView中的各种坐标转换convertPoint:toView OR convertPoint:fromView
来源:互联网 发布:且夫知不知 编辑:程序博客网 时间:2024/06/06 18:46
开发过程中,我们经常会判断A和B之间是否相交或者AB之间是否相互包含,系统封装了
一些方法供我们使用
CGRectContainsRect(<#CGRect rect1#>, <#CGRect rect2#>)CGRectContainsPoint(<#CGRect rect#>, <#CGPoint point#>)CGRectIntersectsRect(<#CGRect rect1#>, <#CGRect rect2#>)
1.表示rect1和rect2是否相交2.表示rect中是否包含point坐标
3.表示rect1中是否包含rect2
那么当坐标不确定的情况下,如何将多层级的View相互转换坐标呢,如下图所示,给大
家简单介绍下方法
- (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;
以这个图片为例
FromView
CGRect rec = [self.view convertRect:self.yellowView.frame fromView:self.greyView]; NSLog(@"%@",NSStringFromCGRect(rec));
InView
CGRect newRect = [self.yellowView convertRect:CGRectMake(50, 50, 20, 20) toView:self.blueView];
需要注意的是:ToView是可以传nil的
CGRect newRect = [self.yellowView convertRect:CGRectMake(50, 50, 20, 20) toView:nil];
下面来展示三个方法如何做到黄色View在Window中的位置
前两种(ToView)
CGRect newRect = [self.yellowView convertRect:self.yellowView.bounds toView:nil]; CGRect newRect = [self.greyView convertRect:self.yellowView.frame toView:nil];
这样也正好表明了frame和bounds的区别,第一段代码表示,在yellowView中,有个
和yellowView一样的大小的区域bounds,此区域相对于window的位置,而第二段代码
表示,在greyView中,其子视图yellowView的frame区域,相对于window的位置
第三种(FromView)
CGRect rec1 = [self.view.window convertRect:self.yellowView.bounds fromView:self.yellowView];
总结:
toView就是从左往右开始读代码,也是从左往右理解意思
fromView就是从右往左开始读代码,也是从右往左理解意思
- A(CGPoint)convertPoint:B(CGPoint)point toView:C(nullableUIView *)view;
- A(CGPoint)convertPoint:B(CGPoint)point fromView:C(nullableUIView *)view;
第一句代表A区域里面有个坐标B,需要把相对于A的坐标B转换成相对于C的坐标
第二句代表
从C区域里面转换坐标B,需要把相对于C的坐标转换成相对于A的坐标
理解下用起来真的蛮不错哦
2 0
- UVA 562 Dividing coins (01背包)
- [HDU 4609] 3-idiots (计数+FFT优化卷积)
- 【学习笔记】抽象类
- 视频播放功能模块
- 大整数-综述-加法
- UIView中的各种坐标转换convertPoint:toView OR convertPoint:fromView
- HTML5基础篇之html(二)
- linux qcom LCD framwork
- 如何由RGB计算HSV空间的色相值
- Android——数据存储(四种方式之二)读写SD卡
- CSS包含块(Containing Block)
- 【学习笔记】接口
- 高性能协程库State Thread Library源代码剖析之一Makefile详解
- 飞行路线_bzoj2763_分层图+spfa