CALayer( 一 )
来源:互联网 发布:linux 进程stdio 编辑:程序博客网 时间:2024/05/17 06:11
一 CALayer是什么?
摘自官网的一句话-Layers Provide the Basis for Drawing and Animations(Layers是绘图和动画的基础)
Layer是在3D空间中的2D平面。Layer管理的几何(例如rotate,transfrom),内容(image等),和可视属性(backgroundColor,alpha)等信息。Layer主要通过管理bitmap来维护自己的状态信息,从这一点上来说,Layer可以看作对象模型,因为他们主要用来管理数据。
Layer是基于bitmap的,它会捕获View要呈现的内容,然后cache在一个bitmap中,这个bitmap可以看作一个对象。这样每次进行操作,例如平移旋转等,只是bitmap的矩阵运算。基于Layer的动画过程如图
由于基于Layer的绘制是处理静态的Bitmap的,而bitmap的处理又是GPU所擅长的,所以它的效率要比基于View绘制的高很多,因为基于View绘制的每次都要进行drawRect的调用重新绘制。
二 Layer支持继承,支持添加Sublayer,支持对sublayer进行层次调整
常用的Layer子类
CAEmitterLayer
发射器层,用来控制粒子效果
CAGradientLayer
梯度层,颜色渐变
CAEAGLayer
用OpenGL ES绘制的层
CAReplicationLayer
用来自动复制sublayer
CAScrollLayer
用来管理可滑动的区域
CAShapeLayer
绘制立体的贝塞尔曲线
CATextLayer
可以绘制AttributeString
CATiledLayer
用来管理一副可以被分割的大图
CATransformLayer
用来渲染3D layer的层次结构
管理Layer内容的几个函数
addSublayer:
insertSublayer:above:
insertSublayer:atIndex:
insertSublayer:below:
replaceSublayer:with:
三 直接设置UIView的Layer
先看一个示例,然后我会列出常用的属性,最后就某几个比较不容易理解的属性单独分析。
先在Stroyboard上拖拽一个UIView,然后control+drag出一个IBOutlet,命名为containView
然后,在ViewDidLoad中,键入如下代码这样,运行后的效果如图
四 添加Sublayer
效果图如图有可能添加Sublayer的时候,sublayer的frame范围已经超过了super Layer的frame,那么会怎么样呢?
修改sublayer1的位置,然后效果如图但是,很多时候我们并不想sublayer的范围超出 super layer,这时候可以设置这个属性
效果如图这里再听过两个常用的CALayer的子类UIShapeLayer和UITextLayer的示例
效果图如图五 anchorPoint和position
和UIView不同,Layer主要由三个属性来设置位置(极少用Frame):
bounds - 设置大小
anchorPoint -设置锚点(锚点对后续的layer动画有很大影响)
position - 锚点在superLayer中的位置
这样说有点抽象,我们看看以下的图就了解了
对于iOS来说,坐标系如图,archPoint(x,y)的两个值通常取0.0-1.0,默认值是(0.5,0.5)这里的值可以看作所占用x的比例,比如默认的0.5,0.5就是在x的中间和y的中间。
而position则是AnchorPoint在super layer中的位置
如下图
五 Layer显示图片
效果图
这里,要详细讲解以下contentGravity这个属性。这个属性决定了contents如何填充。
具体分为两个方面,
方面一,位置方面
具体如图
方面二,比例变换方面
如图
六 Layer于UIView的区别
摘自官方文档
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.In iOS, every view is backed by a corresponding layer object but in OS X you must decide which views should have layers. In OS X v10.8 and later, it probably makes sense to add layers to all of your views. However, you are not required to do so and can still disable layers in cases where the overhead is unwarranted and unneeded. Layers do increase your app’s memory overhead somewhat but their benefits often outweigh the disadvantage, so it is always best to test the performance of your app before disabling layer support.
简单来说,View和Layer最大的区别就是View可以接受用户输入(例如触摸)而Layer不可以,Layer单独并不能呈现出任何可视的内容,必须依托于View。Layer只是几何上呈现给用户的东西,它较为轻量,通常采用Cache技术,对资源消耗也较小。
七 接下来的计划
下一篇会继续更新CALayer,更新一些不常用的属性和方法,然后更新Core Animation的内容
- CALayer( 一 )
- calayer动画总结(一)
- CALayer简介 (一)功能
- CALayer 学习日记(一)
- IOS SDK详解之CALayer(一)
- IOS SDK详解之CALayer(一)
- core animation初识之CALayer(一)
- CALayer实例一(时钟)
- CALayer-简介(一)
- CALayer Animation实践(一):让应用灵动起来!
- iOS之CALayer与核心动画(一)
- Core Animation实战一(认识图层CALayer)
- 动画—CALayer的初步学习一:CALayer属性—隐式动画(学会从博客上自学)
- CALayer简介 (五)创建自定义CALayer
- IOS CALayer(一)彻底理解CALayer的position与anchorPoint
- CALayer 详解(转载)
- 图层(CALayer)
- CALayer使用(1)
- iOS应用设计模式开发中对简单工厂和工厂方法模式的运用
- 三个线程ABC,交替打印ABC
- 题目:Opencv中的点追踪技术
- 01.在PL/SQL中调用存储过程--oracle
- Newtown Slots "The Discovery" Adventure Slot Machine!(ibet, newtoawn casino malaysia, newtown, Newt)
- CALayer( 一 )
- 2017/4/11js中return关键字的作用
- IO复用、多进程和多线程三种并发编程模型
- ios修改应用名称以及修改默认的语言为中文
- 如何查找QUERY 与 WORKBOOK的关系
- 高性能高可用(3)NGINX
- Enhanced ABAP Development with Core Data Services (CDS)
- Ubuntu 16.04全新特性和改进大盘点:软件中心没了
- Memcache,Redis,MongoDB(数据缓存系统)方案对比与分析