CALayer 和 UIView
来自苹果官方的描述:
An object that manages image-based content and allows you to perform animations on that content,Layers are often used to provide the backing store for views but can also be used without a view to display content.A layer’s main job is to manage the visual content that you provide but the layer itself has visual attributes that can be set, such as a background color, border, and shadow. .
CALayer 通常是做为UIView类的属性进行使用,但也可以单独使用。实际上CALayer是属于QuartzCore框架,而这个框架是一个跨平台的绘制框架。这里的跨平台指的是在iOS和macOS系统上均能使用,也就是说CALayer能在iOS和macOS上面绘制内容(这也说明了为什么CALayer的很多属性都不是UIKit框架下面的东西,比如它的backgroundColor是CGColorRef,因为macOS中没有UIKit)。但是这两个平台接收用户交互的方式完全不一样:iOS是通过触摸事件(touch event)而macOS则是监听鼠标和键盘事件。
UIView负责处理用户交互,负责绘制内容的则是它持有的那个CALayer,我们访问和设置UIView的这些负责显示的属性实际上访问和设置的都是这个CALayer对应的属性,UIView只是将这些操作封装起来了而已。
Layers能够有子Layers,就像视图可以有子视图一样。Layers能够动画化。当你想改变一个Layers的属性时后,你可以使用CAAnimation去为这些改变增加动画效果Layers相对View来说是轻量的