美文网首页
iOS~ frame 和 bounds 的区别和联系

iOS~ frame 和 bounds 的区别和联系

作者: 派大星的博客 | 来源:发表于2018-10-12 18:13 被阅读1次

    推荐文章
    Understanding UIScrollView

    1、 frame 和 bounds 的区别和联系

    frame && bounds.jpeg

    frame: 该view在父view坐标系统中的位置和大小。(参照点是,父亲的坐标系统)

    bounds

    • The bounds rectangle, which describes the view’s location and size in its own coordinate system.
    • 该view在本地坐标系统中的位置和大小。(参照点是,本地坐标系统,就相当于ViewB自己的坐标系统,以0,0点为起点)

    center:该view的中心点在父view坐标系统中的位置和大小。(参照点是,父亲的坐标系统)


    解释:
    frame:frame的(frame.origin.x,frame.origin.y)就是相对于父坐标系的偏移量。

    bounds: 每个view都有一个本地坐标系统。当然bounds这个属性也是参照这个本地坐标系统来的。

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

    这里就涉及到了center的知识了,iOS是以center为基准的,不像其他平台以左上角为基准。摘自ios开发文档:center:描述当前视图的中心点在其父视图中的位置。frame、bounds和center三个属性是相互关联、相互影响的,其中一个属性发生变化,其他属性也会跟着变化。一般使用center来改变(移动)视图的位置(也可用frame来改变位置)。另外,对视图进行旋转、缩放也都是相对于center进行操作的。


    2、bounds的重要作用:✨UIScrollView的滚动效果是根据bounds的改变完成的✨

    uikit-coordinate-system-bounds-animation.gif
    UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 100, 100)];
    redView.backgroundColor = [UIColor colorWithRed:0.815 green:0.007
        blue:0.105 alpha:1];
    
    UIView *greenView = [[UIView alloc] initWithFrame:CGRectMake(150, 160, 150, 200)];
    greenView.backgroundColor = [UIColor colorWithRed:0.494 green:0.827
        blue:0.129 alpha:1];
    
    UIView *blueView = [[UIView alloc] initWithFrame:CGRectMake(40, 400, 200, 150)];
    blueView.backgroundColor = [UIColor colorWithRed:0.29 green:0.564
        blue:0.886 alpha:1];
    
    UIView *yellowView = [[UIView alloc] initWithFrame:CGRectMake(100, 600, 180, 150)];
    yellowView.backgroundColor = [UIColor colorWithRed:0.972 green:0.905
        blue:0.109 alpha:1];
    
    [mainView addSubview:redView];
    [mainView addSubview:greenView];
    [mainView addSubview:blueView];
    [mainView addSubview:yellowView];
    

    通过改变mainView.bounds.origin来实现上图的效果,具体请参看Understanding UIScrollView

    相关文章

      网友评论

          本文标题:iOS~ frame 和 bounds 的区别和联系

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