zoukankan      html  css  js  c++  java
  • iOS开发 frame 与 bounds 的区别与关系 转自隔叶黄莺

    frame和bounds是UIView中的两个属性(property)。

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

    bounds指的是:该view在本身坐标系统中 的位置和大小。(参照点是本身坐标系统)

    -(CGRect)frame{
      return CGRectMake(self.frame.origin.x,self.frame.origin.y,self.frame.size.width,self.frame.size.height);
    }
    -(CGRect)bounds{
      return CGRectMake(0,0,self.frame.size.width,self.frame.size.height);
    }

    基本概念:

    frame: 该view在父view坐标系统中的位置和大小。(参照点是,父亲的坐标系统)
    bounds:该view在本地坐标系统中的位置和大小。(参照点是,本地坐标系统)
    center:该view的中心点在父view坐标系统中的位置和大小。(参照点是,父亲的坐标系统)
    实际上只有bounds和center两个属性。frame是为了方便直观多加的属性。所以修改了一个可能会影响到其他属性。文档中如下说:
    Although you can set the values of these properties independently, setting the value for one changes the others in t
    he following ways:
    When you set the frame property, the size of the bounds property is set to match the size of the frameproperty. The center property is also adjusted to match the center point of the new frame.
    When you set the center property, the origin of the frame changes accordingly.
    When you set the size of the bounds rectangle, the size of the frame rectangle changes to match。
     
    前两个很明显,最后一个bounds稍微有点费解。这里一定要顺便说下本地坐标系统:每个view都有一个本地坐标系统。这个坐标系统作用比较重要,比如触摸的回调函数中的UITouch里面的>坐标值都是参照这个本地坐标系统的坐标。当然bounds这个属性也是参照这个本地坐标系统来的。其实本地坐标系统的关键就是要知道的它的原点(0,0)在什么位置(这个位置又是相对于上层的view的本地坐标系统而言的,当然最上面的一层view就是 window它的本地坐标系统原点就是屏幕的左上角了)。通过修改view的bounds属性可以修改本地坐标系统的原点位置。
     
    修改bounds测试结果:
    bounds (0,0,100,100) --> (0,0,200,200) 本地坐标系统原点往左往上分别50。center不变,效果是按照中心放大view
    bounds(0,0,100,100) --> (100,100,100,100) 本地坐标系统原点往左往上分别100。center不变,没有可视效果变化(但是本地坐标系统的原点已经改变)
     
    结论:
    bounds属性影响到本地坐标系统的原点。需要注意

    本文链接 http://unmi.cc/ios-bounds-frame, 来自 隔叶黄莺 Unmi Blog

    [版权声明]本站内文章,如未特别注明,均系原创或翻译之作,本人 Unmi 保留一切权利。本站原创及译作未经本人许可,不得用于商业用途及传统媒体。网络媒体可随意转载,或以此为基础进行演译,但务必以链接形式注明原始出处和作者信息,否则属于侵权行为。另对本站转载他处文章,俱有说明,如有侵权请联系本人,本人将会在第一时间删除侵权文章。及此说明,重之之重。

    花开花谢春不管,水暖水寒鱼自知.
  • 相关阅读:
    开始使用 UIAlertController 吧
    SegmentControl 那些令人烦恼的事儿
    UIWindow 实现遮盖导航条的蒙版
    C++语言-09-多任务
    C++语言-08-命名空间
    使用 UICollectionView 实现日历签到功能
    C++语言-07-异常处理和信号处理
    Django模板(三)
    数据可视化包
    数据分析核心包
  • 原文地址:https://www.cnblogs.com/taintain1984/p/3408034.html
Copyright © 2011-2022 走看看