iOS_核心动画CALayer(一)
创建视图对象时,视图会自己创建一个层,视图在绘图(如drawRect:)时,会将内容画在自己的层上。当视图在层上完成绘图后,系统会将图层拷贝至屏幕。每个视图都有一个层,而每个图层又可以有多个子层。
提示:
1.Layer的设计目的不是为了取代视图,因此不能基于CALayer创建一个独立的可视化组件
2.Layer的设计目的是提供视图的基本可视内容,从而提高动画的执行效率
3.除提供可视内容外,Layer不负责视图的事件响应、内容绘制等工作,同时Layer不能参与到响应者链条中
三、CALayer的使用说明
- 通过UIView的layer属性可以拿到对应的根层,这个层不允许重新创建,但可以往层里面添加子层(调用CALayer的addSublayer)
- 要具体使用CALayer,需要引入<QuartzCore/QuartzCore.h>
- 获取当前图层或使用静态方法layer初始化CALayer后,可以设置以下属性:
- bounds:宽度和高度
- position:位置(默认指中心点,具体由anchorPoint决定)
- anchorPoint:锚点(x,y的范围都是0-1),决定了position的含义
- backgroundColor: 背景颜色(CGColorRef类型)
- borderColor:边框颜色(CGColorRef类型)
- borderWidth:边框宽度
- cornerRadius:圆角半径
- contents: 内容(比如设置为图片CGImageRef)
- 注意:虽然CALayer可以使用frame,但最好还是使用bounds和position。为层设置动画时,用bounds和position会方便一点。
下面通过一个案例,来学会运用CALayer的常用属性:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor grayColor];
//设置UIView上CALayer的圆角半径
self.view.layer.cornerRadius = ;
self.view.layer.borderWidth = ;
self.view.layer.borderColor = [[UIColor redColor]CGColor];
//创建子层
CALayer *subLayer = [CALayer layer];
subLayer.backgroundColor = [[UIColor magentaColor]CGColor];
//设置圆角半径
subLayer.cornerRadius = ;
subLayer.borderWidth = ;
subLayer.borderColor = [[UIColor blackColor]CGColor];
//阴影的偏移
subLayer.shadowOffset = CGSizeMake(, );
//设置subLayer的阴影的模糊程度
subLayer.shadowRadius = ;
subLayer.shadowColor = [[UIColor blackColor]CGColor];
//设置阴影的透明度
subLayer.shadowOpacity = 0.8;
subLayer.frame = CGRectMake(, , , );
[self.view.layer addSublayer:subLayer];
CALayer *subLayer2 = [CALayer layer];
subLayer2.cornerRadius = ;
subLayer2.borderWidth = ;
subLayer2.borderColor = [[UIColor blackColor]CGColor];
subLayer2.shadowOffset = CGSizeMake(, );
subLayer2.shadowRadius = ;
subLayer2.shadowColor = [[UIColor blackColor]CGColor];
subLayer2.shadowOpacity = 0.8;
subLayer2.frame = CGRectMake(, , , );
[self.view.layer addSublayer:subLayer2];
CALayer *imageLayer = [CALayer layer];
//imageLayer层显示的内容
imageLayer.contents = (id)[UIImage imageNamed:@"5.jpg"];
imageLayer.frame = subLayer2.bounds;
[subLayer2 addSublayer:imageLayer];
CALayer *customDrawn = [CALayer layer];
customDrawn.delegate = self;
customDrawn.backgroundColor = [[UIColor greenColor]CGColor];
customDrawn.frame = CGRectMake(, , , );
customDrawn.shadowOffset = CGSizeMake(, );
customDrawn.shadowRadius = 5.0;
customDrawn.shadowColor = [[UIColor blackColor]CGColor];
customDrawn.shadowOpacity = 0.8;
customDrawn.cornerRadius = 10.0;
customDrawn.borderColor = [[UIColor blackColor]CGColor];
customDrawn.borderWidth = 2.0;
customDrawn.masksToBounds = YES;
[self.view.layer addSublayer:customDrawn];
[customDrawn setNeedsDisplay];
}
-(void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
UIColor *bgColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"6.jpg"]];
CGContextSetFillColorWithColor(ctx, [bgColor CGColor]);
CGContextFillEllipseInRect(ctx, CGRectMake(, , , ));
CGContextFillPath(ctx);
CGContextSetRGBStrokeColor(ctx, , , , );
CGContextStrokePath(ctx);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
运行结果如下图:

四、CALayer的隐式动画属性
- 每一个UIView内部都默认关联着一个CALayer,称这个Layer为Root Layer。所有的非Root Layer都存在着隐式动画,隐式动画的默认时长为1/4秒。
- 当修改非Root Layer的部分属性时,相应的修改会自动产生动画效果,能执行隐式动画的属性被称为“可动画属性”,诸如:
- bounds: 缩放动画
- position: 平移动画
- opacity: 淡入淡出动画(改变透明度)
- 在文档中搜素animatable可以找到所有可动画属性
- 如果要关闭默认的动画效果,可以通过动画事务方法实现:
[CATransaction begin];//开启事务
[CATransaction setDisableActions:YES];//取消隐式动画
#import "ViewController.h" @interface ViewController ()
@property(strong,nonatomic)CALayer *layer;
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// 创建子层
self.layer = [[CALayer alloc]init];
//设置子层的bounds
_layer.bounds = CGRectMake(, , , );
//设置背景颜色
_layer.backgroundColor = [[UIColor blueColor]CGColor];
//设置位置
_layer.position = CGPointMake(, ); [self.view.layer addSublayer:_layer];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//事务开始
[CATransaction begin];
// //取消隐式动画
// [CATransaction setDisableActions:YES];
//设置隐式动画的时长(如果取消隐式动画,则不起作用)
[CATransaction setAnimationDuration:1.0]; UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self.view];
_layer.position = location;
//视图提交
[CATransaction commit]; } @end
五、在CALayer上绘图
要在CALayer上绘图,有两种方法:
(1)创建一个CALayer的子类,然后覆盖drawInContext:方法,可以使用Quartz2D API在其中进行绘图
(2)设置CALayer的delegate,然后让delegate实现drawLayer:inContext:方法进行绘图
注意:
(1)不能再将UIView设置为这个CALayer的delegate,因为UIView对象已经是内部层的delegate,再次设置会出问题
(2)无论使用哪种方法,都必须向层发送setNeedsDisplay消息,以触发相应绘图方法的调用
#import "MyLayer.h" @implementation MyLayer
-(void)drawInContext:(CGContextRef)ctx
{
CGContextAddRect(ctx, CGRectMake(, , , ));
CGContextSetRGBFillColor(ctx, , , , );
CGContextDrawPath(ctx, kCGPathFillStroke);
}
@end
再看下实现文件的代码:
#import "ViewController.h"
#import "MyLayer.h"
@interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// 创建子层
MyLayer *subLayer = [[MyLayer alloc]init];
//设置子层的背景颜色
subLayer.backgroundColor = [[UIColor redColor]CGColor];
//设置子层bounds
subLayer.bounds = CGRectMake(, , , );
//设置子层的位置
subLayer.position = CGPointMake(, );
//将子层添加到view层中
[self.view.layer addSublayer:subLayer];
//当重绘的时候会调用此方法
[subLayer setNeedsDisplay];
} @end
第二种:使用代理
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 创建子层
CALayer *subLayer = [[CALayer alloc]init];
//设置bounds;
subLayer.bounds = CGRectMake(, , , );
//设置位置
subLayer.position = CGPointMake(, );
//设置锚点
subLayer.anchorPoint = CGPointMake(0.5, 0.5);
//设置背景色
subLayer.backgroundColor = [[UIColor blueColor]CGColor];
//将创建的子层,添加到view的子层中
[self.view.layer addSublayer:subLayer];
//设置代理
subLayer.delegate = self;
//重绘时调用此方法
[subLayer setNeedsDisplay];
}
#pragma mark - CALayer的代理方法
-(void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
//绘制图形
CGContextAddRect(ctx, CGRectMake(, , , ));
//设置图形的填充颜色
CGContextSetRGBFillColor(ctx, , , , );
CGContextDrawPath(ctx, kCGPathFillStroke);
//绘制图片
UIImage *image = [UIImage imageNamed:@"1.png"];
//旋转图片
CGContextScaleCTM(ctx, , -);
CGContextTranslateCTM(ctx, ,-);
//绘制图片
CGContextDrawImage(ctx, CGRectMake(, , , ),[image CGImage]);
}
@end
六、总结
1.CALayer、UIView以及上下文的之间的关系
(1)当UIView收到setNeedsDisplay消息时,CALayer会准备好一个CGContextRef,然后向它的delegate即UIView发送消息,并且传入已经准备好的CGContextRef对象。UIView在drawLayer:inContext:方法中会调用自己的drarRect:方法。
(2)平时在drawRect:中通过UIGraphicsGetCurrentContext()获取的就是有CALayer传入的CGContextRef对象,在drawRect:中完成的所有绘图都会填入CALayer的CGContextRef中,然后被拷贝至屏幕
(3)CALayer的CGContextRef用的是位图上下文(Bitmap Grahpics Context)。
iOS_核心动画CALayer(一)的更多相关文章
- iOS_核心动画(二)
目 录: 一.Core Animation开发步骤 二.Core Animation的继承结构 三.CAAnimation常用的属性 四.CAPropertyAnimation(属性动画) 五.CAB ...
- iOS核心动画 - CALayer
大家知道,在iOS中所有的视图都继承自UIView. UIView处理所有的触摸事件和画图. 事实上,UIView所有的渲染和动画是托管给另一个类来负责的,它就是CALayer. 但是,需要记住的是, ...
- iOS核心动画CALayer和UIView
UIView和CALayer的关系. 每一个UIview都有一个CALayer实例的图层属性,也就是所谓的backing layer. 实际上这些背后关联的图层才是真正用来在屏幕上显示和做动画,UIV ...
- iOS开发——UI进阶篇(十七)CALayer,核心动画基本使用
一.CALayer简介 1.CALayer在iOS中,文本输入框.一个图标等等,这些都是UIView你能看得见摸得着的东西基本上都是UIView,比如一个按钮.一个文本标签.一个其实UIView之所以 ...
- iOS:CALayer核心动画层
CALayer:核心动画层 简介: Core Animation 是跨平台的,支持iOS环境和Mac OS X环境 学习核心动画之前,需要先理解CALayer,因为核心动画操作的对象不是UIView, ...
- iOS核心动画高级技巧之CALayer(一)
iOS核心动画高级技巧之CALayer(一) iOS核心动画高级技巧之图层变换和专用图层(二)iOS核心动画高级技巧之核心动画(三)iOS核心动画高级技巧之性能(四)iOS核心动画高级技巧之动画总结( ...
- iOS开发UI篇—核心动画(UIView封装动画)
iOS开发UI篇—核心动画(UIView封装动画) 一.UIView动画(首尾) 1.简单说明 UIKit直接将动画集成到UIView类中,当内部的一些属性发生改变时,UIView将为这些改变提供动画 ...
- iOS开发UI篇—核心动画(关键帧动画)
转自:http://www.cnblogs.com/wendingding/p/3801330.html iOS开发UI篇—核心动画(关键帧动画) 一.简单介绍 是CApropertyAnimatio ...
- iOS开发UI篇—核心动画(基础动画)
转自:http://www.cnblogs.com/wendingding/p/3801157.html 文顶顶 最怕你一生碌碌无为 还安慰自己平凡可贵 iOS开发UI篇—核心动画(基础动画) iOS ...
随机推荐
- IOS 键盘协议之中的一个 <UITextFieldDelegate>
1. 设置键盘的第一响应者后,便可通过点击TextField唤出键盘 设置键盘第一响应者方法为: [textField becomeFirstResponder];//此时,textField 输入框 ...
- git分支管理与冲突解决(转载)
Git 分支管理和冲突解决 原文:http://www.cnblogs.com/mengdd/p/3585038.html 创建分支 git branch 没有参数,显示本地版本库中所有的本地分支名称 ...
- FreeMarker / S2SH 各种报错解决方案
1. org.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling setter of ...
- jQuery整理笔记八----jQuery的Ajax
Ajax,我一直读的是"阿贾克斯",据当时大学老师讲该读音出处是依据当年风靡欧洲的荷兰足球俱乐部阿贾克斯的名字来的,我认为说法挺靠谱的. jQuery封装了Ajax的交互过程,用户 ...
- vue起手式
主要步骤 安装node 安装npm 安装vue-cli(vue命令行工具) 初始化一个vue项目 进行开发 # 安装node # 安装npm # 安装cnpm,在中国大陆防止被墙 # 安装git # ...
- XML 文档的结构
XML 文档的组成 一个XML文档由两部分构成:第一部分是文档序言,第二部分是文档元素(节点). 1.文档序言 文档序言通常位于XML文档的顶端,根元素之前出现,它是一个特定的包含XML 文档设定信息 ...
- flex 均分铺满
<view>充值金额</view> <view class="weui-flex"> <repeat for="{{amount ...
- Android系统移植与调试之------->如何修改开机动画的两种方式剖析
首先,我们先来分析一下源码: frameworks/base/cmds/bootanimation/BootAnimation.cpp 首先看一下定义的常量: BootAnimation::ready ...
- A Simple Web Server
介绍 在过去20几年里,网络已经在各个方面改变了我们的生活,但是它的核心却几乎没有什么改变.多数的系统依然遵循着Tim Berners-Lee在上个世纪发布的规则.大多数的web服务器都在用同样的方式 ...
- app开发公司排名哪家强?看App Annie给出的答案
app开发公司排名哪家强?这个答案不好定义,我们从第三方权威平台数据来看吧.App Annie在<全球移动应用市场2016年回顾>报告中从全球每月活跃用户数.全球下载量.全球收入等几个维度 ...