iOS学习笔记:frame,bound,center, anchorPoint

frame: View在它的Super View坐标系里的坐标

bound: 用来定义View自身坐标系和边界的Rect,Rect的原点表示View自身坐标系的原点坐标。举个例子:

  一般情况下bound的值为(0,0,width,heigh),其中0,0表示View自身坐标系的原点坐标为(0,0)

  但是既然类型是Rect,我也可以设置bound的原定为非(0,0)啊,比如bound值为(10,10,width,height). 这样则表示View自身的坐标系中原点的坐标为(10,10).这对于View自身其实没有影响,但对于View的subView,他们的显示位置可能就要用frame的值减去这个原点的值了。看如下代码:

class LayerViewController: UIViewController {

    private var subLayer : CALayer!;
    
    override func viewDidLoad() {
        super.viewDidLoad()
        let subView = UIView();
        subView.backgroundColor = UIColor.blueColor();
        subView.bounds = CGRect(x: 50, y: 50,  50, height: 50);
        subView.frame = CGRect(x: 100, y: 100,  50, height: 50);
        
        let thirdView = UIView();
        thirdView.backgroundColor = UIColor.redColor();
        thirdView.frame = CGRect(x: 50, y: 50,  10, height: 10);
        subView.addSubview(thirdView);
        self.view.addSubview(subView);
    }
}

  UI呈现效果:

虽然thirdView的frame设为(50,50,50,50),但是因为subView的bound原点为(50,50).因此thirdView显示在subView的左上角。

anchorPoint:

其实是CALayer中的属性没有在View中暴露出来。因此我们如果需要修改需要通过修改View的关联Layer的anchorPoint属性来实现。anchorPoint是用来确定在做旋转,放大等操作时用来做参考点的点。他的值是一个相对与bound的的值,在bound内,则取值范围为(0,0)~(1,1)

center:

表示View的anchorPoint在super view的坐标系中的位置。

frame与bound都能这只View的weight和height, 那可以设置不一样吗?答案是肯定的。其实frame是一个虚拟属性,它的值是通过bound和center计算出来的。同时当给frame赋值时,bound和center的值也会被重设。

原文地址:https://www.cnblogs.com/Code-life/p/6019081.html