文章转载自40K CLUB APP

一、CALayer官方文档

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. In addition to managing visual content, the layer also maintains information about the geometry of its content (such as its position, size, and transform) that is used to present that content onscreen. Modifying the properties of the layer is how you initiate animations on the layer’s content or geometry. A layer object encapsulates the duration and pacing of a layer and its animations by adopting the CAMediaTiming protocol, which defines the layer’s timing information.
If the layer object was created by a view, the view typically assigns itself as the layer’s delegate automatically, and you should not change that relationship. For layers you create yourself, you can assign a delegate object and use that object to provide the contents of the layer dynamically and perform other tasks. A layer may also have a layout manager object (assigned to the layoutManager property) to manage the layout of subviews separately.

二、UIView和CALayer的关系

由官方文档和平时的知识积累和开发经验可知,Layer是View的一个属性,Layer由View初始化时自动创建,而且View会自动将自己设置为Layer的代理。

同时CALayer提供了一个+layerClass方法,我们可以通过重写这个方法返回一个我们自定义的CALayer的子类的类型,这时,UIView会自动创建一个我们自定义子类类型的Layer作为View的根Layer。

+ (Class)layerClass;

三、CALayer的一些属性

1 presentationLayer

通过官方文档可知,Layer的作用是为View提供后备存储,什么意思呢?Layer的主要工作是提供视觉内容,当我们修改Layer的属性时,会启动隐式动画,动画是有过渡时间的。如果一个动画还没有结束,我们提交了新的属性,这时会发生什么?会在前一个动画结束后,才会刷新新的属性。也就是说Layer的属性不是实时显示在屏幕上的,那么我们怎样获取此时屏幕上属性值呢?

可查看官方文档中对presentationLayer的描述,这个值是最接近此时屏幕显示的属性值的Layer。

2 contents

The default value of this property is nil.
If you are using the layer to display a static image, you can set this property to the CGImage containing the image you want to display. (In macOS 10.6 and later, you can also set the property to an NSImage object.) Assigning a value to this property causes the layer to use your image rather than create a separate backing store.

当我们把这个值设置为一个CGImage对象时,Layer就会显示为一张图片。

3 display

这个方法不要主动调用,通过调用Layer的代理的display(_:)方法或者Layer的draw(in:) 方法,会自动调用改方法。这个方法的作用是设置contents属性。我们可以重写这个方法,来设置自定义的contents。

4 setNeedsDisplay

Marks that -display needs to be called before the layer is next committed.

从Xcode的接口声明中可以指导,该方法不会立即调用display方法,而是做一个标记,在Layer下一次提交时,先调用display方法。

5 defaultValue(forKey:)

指定默认属性值。

四、总结

我们了解了以上几个属性和方法的作用,就能轻松的知道YYtext的同步/异步渲染流程了。

文章转载自40K CLUB APP

YYText源码解读-YYText同步/异步渲染流程(一)—UIView与CALayer相关推荐

  1. layer 同步调用_YYText源码解读-YYText同步/异步渲染流程(一)—UIView与CALayer

    一.CALayer官方文档 Layers are often used to provide the backing store for views but can also be used with ...

  2. pyecharts源码解读(10)渲染包render之templates目录:渲染模板

    当前pyecharts版本为1.9.0 概述 render包结构 render包位于pyecharts包顶级目录中,用于渲染图表.render包结构如下: ├─render # 渲染设置包 │ │ d ...

  3. mysql lenenc int_MySQL-NonMySQL同步工具源码解读——确定同步位置

    经过上一节的鉴权过后,程序已经受主库认可,并且可以像主库发起同步请求.在发起请求之前,还有一个可选的步骤:确认同步时间点.同步时间点由两个属性进行标识:Binlog文件名.偏移量.工具支持自定义时间点 ...

  4. 【Unity】 Spine渲染原理解析与源码解读

    Spine渲染原理解析与源码解读 安装环境 从Spine编辑器导出 将资源导入Unity 基础概念 其他相关概念 Spine架构 Spine运行时的各个模块 有状态(Stateful) 和 无状态(S ...

  5. Vue源码解读(五):render和VNode

    Vue 2.0 相比 Vue 1.0 最大的升级就是利用了虚拟DOM. 在 Vue 1.0 中视图的更新是纯响应式的.在进行响应式初始化的时候,一个响应式数据 key 会创建一个对应的 dep,这个 ...

  6. IOCP 网络通讯模型源码解读

    From: http://hi.baidu.com/tsingsing/item/1aa5062fa27791fa50fd87b7 以前写服务器的时候用的是iocp,最近偶然发现windows的 网络 ...

  7. LevelDB源码解读

    LevelDB源码解读 提供的功能 read and write Group commit sequence number delete Atomic Updates 同步写Synchronous W ...

  8. vue如何让一句代码只执行一次_lt;Vue 源码笔记系列4gt;异步更新队列与$nextTick...

    1. 前言 原文发布在语雀: <Vue 源码笔记系列4>异步更新队列与$nextTick · 语雀​www.yuque.com 上一章我们讲到了修改数据是如何触发渲染函数的观察者,最终调用 ...

  9. dva处理_dva 源码解读

    声明 本文章用于个人学习研究,并不代表 dva 团队的任何观点. 原文以及包含一定注释的代码见这里,若有问题也可以在这里进行讨论 起步 为什么是dva? 笔者对 dva 的源代码进行解读,主要考虑到 ...

  10. 当我们谈论Virtual DOM时,我们在说什么——etch源码解读

    etch简介 首先我们有必要介绍一下etch. etch是atom团队下的开源项目,是一套非常简洁然而功能十分完善的virtualDOM机制.我在偶然的情况下接触到了这个开源项目,在读README时为 ...

最新文章

  1. 小逗比机器人聊天接口api
  2. 定义进项税代码缺省值
  3. HDU1066--高精度求阶乘最后非零位
  4. win7美化_Potplayer64位美化版,无棒子的tv推送
  5. android已停止三星,急,android离线打包程序三星下总报 程序 已停止
  6. 感谢你不哀不丧不怜,为平凡的努力喝彩
  7. 频频被关注的 AI,怎样才能用着舒心?
  8. 9篇前沿文章 | 一览肿瘤基因组及多组学思路
  9. 卸载office2016密钥
  10. 为鼓励居民节约用水,自来水公司采取按月用水量分段计费的办法,居民应交水费y(元)与月用水量x(吨)的函数关系式如下(设x>0)。编写程序,输入用户的用水量x(吨),计算并输出用户应该支付的水费y(元)
  11. spring注解开发实例
  12. [id: * L:/* ! R:/*] onUncaughtException(SimpleConnection{channel=[id: *
  13. springboot-shiro-jwt-redis实现用户登录的认证与授权(前后端分离)需要有一定shiro、jwt、redis、springboot基础
  14. 夏雨老师告诉您学习平面设计到底好不好呢?
  15. QNX系统的实时性分析-实时性能测试标准
  16. 《微信小程序》 开源项目
  17. 366AP微信中可以下载企业包
  18. 超文本超链接HTTP协议
  19. 中国公路桥梁管理系统android,中国公路学报
  20. 日本电子货币用户数今夏有望突破一亿

热门文章

  1. echarts图表的内边距_ECharts 图表插件使用整理(图表配置实现)
  2. 网易云音乐添加到html5,网易云音乐怎么把音乐存到云盘_网易云音乐将音乐存入添加到云盘方法介绍_3DM手游...
  3. 应急管理大数据ppt_大数据在应急管理中的应用
  4. 网站数据统计分析工具有哪些?这七个实用工具推荐给你
  5. Redis常用命令大全
  6. CDA备考学习学习笔记——基础知识篇(一)
  7. 网易云通讯与视频_总结_00_资源帖
  8. 手动实现一门图灵完备的编程语言——Brainfuck
  9. ECharts绘制图表
  10. MATLAB-绘图-散点图绘制