UIView与CALayer(层与视图)

一、官方说明

Layers are not a replacement for your app’s views—that is, you cannot create a visual interface based solely on layer objects. Layers provide infrastructure for your views. Specifically, layers make it easier and more efficient to draw and animate the contents of views and maintain high frame rates while doing so. However, there are many things that layers do not do. Layers do not handle events, draw content, participate in the responder chain, or do many other things. For this reason, every app must still have one or more views to handle those kinds of interactions.

layers不是app中views的替代品,这是因为,你不能仅仅通过layer对象创建一个可视化的界面,layers为views提供基础。

明确的是,layers使views能更容易且更高效的绘图、展现属性改变所产生的动画,且在views在处理一些事情时,layers能协助维持frame rates。

然而有很多事是layers做不了,而views能做的。比如layers不能处理事件和成像,尤其是不在响应链中。

由于这些原因,所有的app必须有一个以上的view来处理各种各样的交互。

二、UIView与CALayer关系总结

UIView和CALayer二者缺一不可,没有CALayer,UIView看不到内容;没有UIView,CALayer像什么内容也没有一样。

1、UIView继承UIResponder,而CALayer继承NSObject。所以UIView可以响应事件,CALayerb不能。

2、UIView有layer属性,有layerClass方法,而CALayer类中没有UIView相关属性和方法。UIView主要负责管理内容,而CALayer主要负责渲染、呈现、隐式动画。

1)获取UIView的CALayer属性,从UIView->CALayer。通过重写layerClass方法,change其中CALayer类型。

2)获取UIView的frame、bounds等属性,其实获取的对应CALayer的属性,设置原理也一样。(隐式动画指的是,由于非rootLayer的frame、bounds等属性值改变时,所产生的界面上的渐变过程)

3、UIView和CALayer都采用树结构,superView/superLayer、subView/subLayer、addSubView:/addSubLayer:等。

4、CALayer维护着三份layer tree,分别是presentLayer tree、modelLayer tree、render tree,在做动画的时候,我们修改动画的属性,其实是修改presentLayer的属性值,而最终展示在界面上的其实是提供UIView的modelLayer。

三、CALayer的属性讲解

为什么修改anchorPoint会移动layer的位置?

CALayer的position点是哪一点呢? 

anchorPoint与position有什么关系?

1、基本概念

UIView有frame、bounds和center三个属性。CALayer也有类似的属性,分别为frame、bounds、positionanchorPoint

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

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

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

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

anchorPoint:view中某一点,在view单元坐标空间中的位置(参照bounds.size的比例值)。

如视图的左上角、右下角、中心点,anchorPoint分为为(0,0), (1, 1),(0.5,0.5)。

position:该view中,值为(0.5,0.5)的anchorPoint点,对应在superLayer中的位置。(参照点是,父亲的坐标系统)

position.x = frame.origin.x + 0.5 * bounds.size.width;

position.y = frame.origin.y + 0.5 * bounds.size.height;

2、UIView的center就是CALayer的position。

3、position和anchorPoint的关联

Layers have an implicit frame that is a function of the position, bounds, anchorPoint, and transform properties.

存在计算公式如下:

frame.origin.x = position.x - anchorPoint.x * bounds.size.width;

frame.origin.y = position.y - anchorPoint.y * bounds.size.height;

问题:修改position会改变anchorPoint?或者修改anchorPoint会影响position?

根据代码测试,两者互不影响,受影响的只有frame.origin,也就是layer坐标原点相对superLayer会有所改变。

换句话说,frame.origin由position和anchorPoint共同决定。修改position/anchorPoint,影响frame。修改frame,影响position

原文地址:https://www.cnblogs.com/zhouyi-ios/p/5234476.html