CALayer及其子类
前言:这个系列要更新Core Animation的内容,但是CALayer是Core Animation的基础。
一 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内容的几个函数
三 直接设置UIView的Layer
先看一个示例,然后我会列出常用的属性,最后就某几个比较不容易理解的属性单独分析。
先在Stroyboard上拖拽一个UIView,然后control+drag出一个IBOutlet,命名为containView
@property (weak, nonatomic) IBOutlet UIView *containView;
然后,在ViewDidLoad中,键入如下代码
containView.layer.backgroundColor = [UIColor lightGrayColor].CGColor;//背景色
containView.layer.cornerRadius = 20.0;//圆角
containView.layer.shadowColor = [UIColor blueColor].CGColor;//阴影颜色
containView.layer.shadowOpacity = 0.8;//阴影透明度
containView.layer.shadowOffset = CGSizeMake(3.0, 3.0);//阴影的偏移量
containView.layer.borderColor = [UIColor redColor].CGColor;//边界颜色
containView.layer.borderWidth = 2;//边界宽度
这样,运行后的效果如图

四 添加Sublayer
containView.layer.backgroundColor = [UIColor lightGrayColor].CGColor;
containView.layer.cornerRadius = 20.0;
containView.layer.shadowColor = [UIColor blueColor].CGColor;
containView.layer.shadowOpacity = 0.8;
containView.layer.shadowOffset = CGSizeMake(3.0, 3.0);
containView.layer.borderColor = [UIColor redColor].CGColor;
containView.layer.borderWidth = 2; CALayer * sublayer1 = [CALayer layer];
sublayer1.backgroundColor = [UIColor blueColor].CGColor;
sublayer1.frame = CGRectMake(0, 0,80,80);
sublayer1.anchorPoint = CGPointMake(0.5, 0.5);
sublayer1.position = CGPointMake(100,100);
[containView.layer addSublayer:sublayer1];
效果图如图

有可能添加Sublayer的时候,sublayer的frame范围已经超过了super Layer的frame,那么会怎么样呢?
sublayer1.position = CGPointMake(0,CGRectGetMaxY(containView.bounds)-10);
修改sublayer1的位置,然后效果如图

但是,很多时候我们并不想sublayer的范围超出 super layer,这时候可以设置这个属性
containView.layer.masksToBounds = YES;
效果如图

这里再听过两个常用的CALayer的子类UIShapeLayer和UITextLayer的示例
CAShapeLayer * shapeLayer = [CAShapeLayer layer];
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path,nil,0.0,0);
CGPathAddLineToPoint(path,nil,0.0,CGRectGetHeight(containView.bounds)/2);
shapeLayer.path = path;
shapeLayer.bounds = CGRectMake(0,0,5.0,CGRectGetHeight(containView.bounds)/2);
shapeLayer.anchorPoint = CGPointMake(0.5, 0.5);
shapeLayer.position = CGPointMake(CGRectGetMidX(containView.bounds),CGRectGetMidY(containView.bounds));
shapeLayer.lineWidth = 5.0;
shapeLayer.lineCap = kCALineCapRound;
shapeLayer.strokeColor = [UIColor yellowColor].CGColor;
[containView.layer addSublayer:shapeLayer]; CATextLayer * textLayer = [CATextLayer layer];
NSString * text = @"blog.csdn.net/hello_hwc";
NSAttributedString * attributeString = [[NSAttributedString alloc] initWithString:text];
textLayer.string = text;
textLayer.alignmentMode = @"center";
textLayer.fontSize = 12;
textLayer.foregroundColor = [UIColor brownColor].CGColor;
CGRect bounds;
bounds.origin = CGPointMake(0, 0);
bounds.size = attributeString.size;
textLayer.bounds = bounds;
textLayer.position = CGPointMake(100,100);
[containView.layer addSublayer:textLayer];
效果图如图

五 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显示图片
CALayer * imageLayer = [CALayer layer];
imageLayer.bounds = CGRectMake(0,0,200,100);
imageLayer.position = CGPointMake(200,200);
imageLayer.contents = (id)[UIImage imageNamed:@"lichen.jpg"].CGImage;
imageLayer.contentsGravity = kCAGravityResizeAspect;
[containView.layer addSublayer:imageLayer];
效果图

这里,要详细讲解以下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技术,对资源消耗也较小。
转载:http://doc.okbase.net/Hello_Hwc/archive/123447.html
CALayer及其子类的更多相关文章
- CALayer的子类之CAShapeLayer
一,CAShapeLayer介绍 * CAShapeLayer继承自CALayer,属于QuartzCore框架,可使用CALayer的所有属性. CAShapeLayer是在坐标系内绘制贝塞尔曲 ...
- iOS CALayer应用详解
跟着大神一起进步,本篇博客原文地址:http://blog.csdn.net/hello_hwc?viewmode=contents 一 CALayer是什么? Layers是绘图和动画的基础, L ...
- CALayer 4 详解 -----转自李明杰
CALayer4-自定义层 本文目录 一.自定义层的方法1 二.自定义层的方法2 三.其他 自定义层,其实就是在层上绘图,一共有2种方法,下面详细介绍一下. 回到顶部 一.自定义层的方法1 方法描 ...
- CALayer笔记
1.Core Animation是跨平台的,支持IOS和Mac OS X环境 2.核心动画操作的对象不是UIView而是CALayer,CALayer是核心动画的基础, 可以做圆角.阴影.边框等效果 ...
- iOS:CALayer核心动画层上绘图
在CALayer上绘图: •要在CALayer上绘图,有两种方法: 1.创建一个CALayer的子类,然后覆盖drawInContext:方法,可以使用Quartz2D API在其中进行绘图 2.设置 ...
- OC - 21.CALayer核心要点及实例解析
CALayer基础 CALayer是每一个UI控件的核心,一个UI控件之所以能显示可以说是CALayer的功劳 每一个UI控件默认都为自己创建一个CALayer对象,通过drawRect方法将内容绘制 ...
- iOS基础 - CALayer
一.CALayer简介 Core Animation是跨平台的,支持iOS环境和Mac OS X环境 凡是支持跨平台的框架,都不能直接使用UIKit框架,因为UIKit框架只能应用在iOS而不能用于M ...
- CALayer 进阶
转载自:http://www.cofcool.net/development/2015/06/19/ios-study-note-eight-CALayer-info/ The CALayer cla ...
- [iOS Animation]-CALayer 专用图层
专用图层 复杂的组织都是专门化的 Catharine R. Stimpson 到目前为止,我们已经探讨过CALayer类了,同时我们也了解到了一些非常有用的绘图和动画功能.但是Core Animati ...
随机推荐
- Could not load file or assembly '$SharePoint.Project.AssemblyFullName$'
The fix is simple, do the following: 1. Open your project file in NotePad 2. Find the PropertyGrou ...
- python 简单搭建非阻塞式单进程,select模式,epoll模式服务
由于经常被抓取文章内容,在此附上博客文章网址:,偶尔会更新某些出错的数据或文字,建议到我博客地址 : --> 点击这里 可以看我的上篇文章 <python 简单搭建阻塞式单进程,多进程, ...
- nginx请求频率限制模块ngx_http_limit_req_module
模块: ngx_http_limit_req_module 作用: 限制客户端请求频率,防止恶意攻击 配置示例: http { limit_req_zone $binary_remote_addr z ...
- 【vim】分割窗口、标签页与Quickfix窗口
vim支持窗口分割和标签页,合适地使用这两种特性可以使文字编辑工作更愉快. 1. 窗口分割 vim支持窗口的水平分割和垂直分割.以下是常用的操作指令或快捷键. 命令 说明 vim -o <fil ...
- MySQL实例crash的案例分析
[作者] 王栋:携程技术保障中心数据库专家,对数据库疑难问题的排查和数据库自动化智能化运维工具的开发有强烈的兴趣. [问题描述] 我们生产环境有一组集群的多台MySQL服务器(MySQL 5.6.21 ...
- JSP知识汇总
JSP知识汇总 一.简介 > HTML - HTML擅长显示一个静态的网页,但是不能调用Java程序. > Servlet - Servlet擅长调用Java程序和后台进行交互,但是它不擅 ...
- margin 和 padding
一图胜千言!!  参考 CSS padding margin border属性详解
- FlexMonkey实战
文章来源:http://www.cnblogs.com/raol/p/flexmonkey.html 我的有道云笔记:http://note.youdao.com/share/?id=22b79669 ...
- day 31 html(二) 和css入门
前情提要: 本次主要是继续昨天学的简单的html 补充以及 css的简单入门 一:表单标签 >1:get请求 <!DOCTYPE html> <html lang=" ...
- Linux 基础命令 持续更新中...
1.ls 显示当前文件/文件夹 显示文件大小: ls -lh 显示隐藏文件: ls -a 显示文件详细信息: ls -l (ll)2.pwd 显示当前所在路径 cat 显示当前文件下所有内容3.cd ...