蛋疼的frame和bounds

2019-02-19  本文已影响0人  二猪哥

frame: 该view在父view坐标系统中的位置和大小。(参照点是,父亲的坐标系统)
bounds:该view在本地坐标系统中的位置和大小。(参照点是,本地坐标系统,就相当于view自己的坐标系统,以0,0点为起点)。

其实本地坐标系统的关键就是要知道的它的原点(0,0)在父坐标系统中的什么位置(这个位置是相对于父view的本地坐标系统而言的,最终的父view就是UIWindow,它的本地坐标系统原点就是屏幕的左上角了)。

通过修改view的bounds属性可以修改本地坐标系统的原点位置。
frame我相信大家都理解的比较清楚,但是bounds光是这么说估计大家都很迷糊,那么我们下面来看具体的实例。
一、常规的场景

//创建红色view
    UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
    NSLog(@"log1:%@",NSStringFromCGRect(redView.frame));
    redView.backgroundColor = [UIColor redColor];
    //把红色view添加到控制器view上
    [self.view addSubview:redView];
    
    //创建紫色view
    UIView *purpleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];
    purpleView.backgroundColor = [UIColor purpleColor];
    NSLog(@"log2:%@",NSStringFromCGRect(purpleView.frame));
    //把紫色view添加到红色view上
    [redView addSubview:purpleView];
image.png

输出日志:

2019-02-19 18:15:35.906877+0800 FMDBTest[16308:790296] log1:redView frame:{{100, 100}, {200, 200}}========redView bounds:{{0, 0}, {200, 200}}
2019-02-19 18:15:35.907117+0800 FMDBTest[16308:790296] log2:purpleView frame:{{0, 0}, {80, 80}}========redView bounds:{{0, 0}, {80, 80}}

二、改变redView的bounds的x和y

//设置bounds,x,y分别为20,20,宽高不变
    redView.bounds = CGRectMake(20, 20, 200, 200);
image.png

输出日志:

2019-02-19 18:27:46.299382+0800 FMDBTest[16523:799583] log1:redView frame:{{100, 100}, {200, 200}}========redView bounds:{{0, 0}, {200, 200}}
2019-02-19 18:27:46.299552+0800 FMDBTest[16523:799583] log2:redView frame:{{100, 100}, {200, 200}}========redView bounds:{{20, 20}, {200, 200}}
2019-02-19 18:27:46.299723+0800 FMDBTest[16523:799583] log3:purpleView frame:{{0, 0}, {80, 80}}========redView bounds:{{0, 0}, {80, 80}}

分析

三、上面只是改变了bounds的x和y,若width和height也改呢

redView.bounds = CGRectMake(20, 20, 300, 300);
image.png

输出日志:

2019-02-19 18:26:43.545989+0800 FMDBTest[16496:798558] log1:redView frame:{{100, 100}, {200, 200}}========redView bounds:{{0, 0}, {200, 200}}
2019-02-19 18:26:43.546154+0800 FMDBTest[16496:798558] log2:redView frame:{{50, 50}, {300, 300}}========redView bounds:{{20, 20}, {300, 300}}
2019-02-19 18:26:43.546293+0800 FMDBTest[16496:798558] log3:purpleView frame:{{0, 0}, {80, 80}}========redView bounds:{{0, 0}, {80, 80}}

四、总结

bounds的有以下两个特点:

1. 它可以修改自己坐标系的原点位置,影响“子view”的显示位置。

2. bounds,它可以通过改变宽高,改变自身的frame,进而影响到再父视图的显示位置和大小。
上一篇 下一篇

猜你喜欢

热点阅读