UIView 和 CALayer 的关系

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 的一个封装:

WWDC 2012 - Session 238 - iOS App Performance: Graphics and Animations

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

参考

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • ViewsBecause view objects are the main way your applicati...
    梁光飞阅读 639评论 0 0
  • 挥舞着雀跃的心情 我飞奔 向着梦寐以求的方向 有你的温暖,光明和芳香 迎面而来的绝杀 让我 不再相信 一见钟情的神...
    暗香盈梦阅读 304评论 0 3
  • (毕业大半年,辗转数千里,遍尝人间苦,今日还家去。 ) 如果用一个字概括“累”,如果用一个词概括“心累”。记...
    船舵手阅读 209评论 0 1
  • 在这个世界上,大多数人的生活并不似波澜壮阔的大海;他们的生活更似那湖中的水,平静的如一面镜子,偶有雨滴落...
    极光与太阳风阅读 476评论 0 3