1、关系
On iOS, every UIView is backed by a Core Animation CALayer. Simply speaking,UIView inherit from NSResponder,handle events from users, contains CALayer,which inherit from NSObject,mainly focus on rendering,animation etc.
One thing UIViews provide over CALayers is built-in support for user interaction. They handle hit-testing on touches and other related actions that you would need to build yourself if managing a hierarchy of CALayers. It‘s not that hard to implement this yourself, but it is extra code you‘d need to write when building a CALayer-only interface.
You will often need to access the underlying layers for a UIView when performing more complex animations than the base UIView class allows. UIView‘s animation capabilities have grown as the iOS SDK has matured, but there are still a few things that are best done by interacting with the underlying CALayer.
2、功能
UIView 主要是对显示内容的管理,CALayer 主要侧重显示内容的绘制。
3、动画 (可参考 objc.io - 动画 部分)
在对UIView 的属性修改时不会产生默认动画,而对单个 layer 属性直接修改会产生默认动画。因为UIView 默认情况下禁止了 layer 动画,但是在 animation block 中又重新启用了它们。
4、CALayer
The neat thing about the CALayer class is that it contains a lot of properties that you can set on it to affect the visual appearance, such as:
- The size and position of the layer
- The layer’s background color
- The contents of the layer (an image, or something drawn with Core Graphics)
- Whether the corners of the layers should be rounded
- Settings for applying a drop shadow to the layer
- Applying a stroke around the edges of the layer
- And much more!
The other neat thing about the properties on CALayer is that most of them are animatable.
参考资料:
- http://www.cocoachina.com/ios/20150828/13244.html
- http://objccn.io/issue-12-4/
- http://stackoverflow.com/questions/7826306/what-are-the-differences-between-a-uiview-and-a-calayer
- http://stackoverflow.com/questions/17715530/the-relationship-between-coregraphics-uiviews-and-calayers
- https://www.raywenderlich.com/2502/calayers-tutorial-for-ios-introduction-to-calayers-tutorial