1. 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.
-- The Relationship Between Layers and Views
簡單來說,UIView 是對 CALayer 的一個封裝:
CALayer 負責繪制內容 contents。而 UIView 負責:1. 為 CALayer 提供內容 contents;2. 處理用戶事件,參與響應鏈。
2. CALayer 如何繪制內容
A layer can display a filled background and a stroked border in addition to its image-based contents. The background color is rendered behind the layer’s contents image and the border is rendered on top of that image, as shown in Figure 2-3. If the layer contains sublayers, they also appear underneath the border. Because the background color sits behind your image, that color shines through any transparent portions of your image.
Adding a border and background to a layer
-- Layers Have Their Own Background and Border
3. UIImageView 如何使用 CALayer 繪制圖片
既然直接對 CALayer 的 contents 屬性賦值一個 CGImage 便能顯示圖片,所以 UIImageView 就順利成章地誕生了。實際上 UIImage 就是對 CGImage(或者 CIImage) 的一個輕量封裝。
WWDC 2012: iOS App Performance: Graphics and Animations
-- iOS App Performance: Graphics and Animations