目  录:
一、核心动画简介
二、图层与视图之间的关系
三、CALayer的使用说明
四、CALayer的隐式动画属性
五、在CALayer上绘图
六、总结
 
 
一、核心动画简介
  Core Animation 是跨平台的,支持iOS环境和Mac OS X环境,而CALayer是核心动画的基础,可以帮助开发者做圆角、阴影、边框等效果。我们学习核心动画之前,需要先理解CALayer,因为核心动画操作的对象不是UIView,而是CALayer。对于UIView控件每个内部都有一个Layer的属性。我们在实现核心动画时,本质上是将CALayer中的内容转换成位图,从而便于图形硬件的操纵。
二、图层和视图之间的关系

  创建视图对象时,视图会自己创建一个层,视图在绘图(如drawRect:)时,会将内容画在自己的层上。当视图在层上完成绘图后,系统会将图层拷贝至屏幕。每个视图都有一个层,而每个图层又可以有多个子层。

  提示:

  1.Layer的设计目的不是为了取代视图,因此不能基于CALayer创建一个独立的可视化组件

  2.Layer的设计目的是提供视图的基本可视内容,从而提高动画的执行效率

  3.除提供可视内容外,Layer不负责视图的事件响应、内容绘制等工作,同时Layer不能参与到响应者链条中

三、CALayer的使用说明

  • 通过UIView的layer属性可以拿到对应的根层,这个层不允许重新创建,但可以往层里面添加子层(调用CALayer的addSublayer)
  • 要具体使用CALayer,需要引入<QuartzCore/QuartzCore.h>
  • 获取当前图层或使用静态方法layer初始化CALayer后,可以设置以下属性:
  1. bounds:宽度和高度
  2. position:位置(默认指中心点,具体由anchorPoint决定)
  3. anchorPoint:锚点(x,y的范围都是0-1),决定了position的含义
  4. backgroundColor: 背景颜色(CGColorRef类型)
  5. borderColor:边框颜色(CGColorRef类型)
  6. borderWidth:边框宽度
  7. cornerRadius:圆角半径
  8. 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的部分属性时,相应的修改会自动产生动画效果,能执行隐式动画的属性被称为“可动画属性”,诸如:
  1. bounds: 缩放动画
  2. position: 平移动画
  3. opacity: 淡入淡出动画(改变透明度)
  • 在文档中搜素animatable可以找到所有可动画属性
  • 如果要关闭默认的动画效果,可以通过动画事务方法实现:

  [CATransaction begin];//开启事务

  [CATransaction setDisableActions:YES];//取消隐式动画

  [CATransaction commit];//事务提交
#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消息,以触发相应绘图方法的调用

   第一种:使用子类
  首先创建一个继承于CALayer的MyLayer类,覆盖drawInContext:方法
#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(一)的更多相关文章

  1. iOS_核心动画(二)

    目 录: 一.Core Animation开发步骤 二.Core Animation的继承结构 三.CAAnimation常用的属性 四.CAPropertyAnimation(属性动画) 五.CAB ...

  2. iOS核心动画 - CALayer

    大家知道,在iOS中所有的视图都继承自UIView. UIView处理所有的触摸事件和画图. 事实上,UIView所有的渲染和动画是托管给另一个类来负责的,它就是CALayer. 但是,需要记住的是, ...

  3. iOS核心动画CALayer和UIView

    UIView和CALayer的关系. 每一个UIview都有一个CALayer实例的图层属性,也就是所谓的backing layer. 实际上这些背后关联的图层才是真正用来在屏幕上显示和做动画,UIV ...

  4. iOS开发——UI进阶篇(十七)CALayer,核心动画基本使用

    一.CALayer简介 1.CALayer在iOS中,文本输入框.一个图标等等,这些都是UIView你能看得见摸得着的东西基本上都是UIView,比如一个按钮.一个文本标签.一个其实UIView之所以 ...

  5. iOS:CALayer核心动画层

    CALayer:核心动画层 简介: Core Animation 是跨平台的,支持iOS环境和Mac OS X环境 学习核心动画之前,需要先理解CALayer,因为核心动画操作的对象不是UIView, ...

  6. iOS核心动画高级技巧之CALayer(一)

    iOS核心动画高级技巧之CALayer(一) iOS核心动画高级技巧之图层变换和专用图层(二)iOS核心动画高级技巧之核心动画(三)iOS核心动画高级技巧之性能(四)iOS核心动画高级技巧之动画总结( ...

  7. iOS开发UI篇—核心动画(UIView封装动画)

    iOS开发UI篇—核心动画(UIView封装动画) 一.UIView动画(首尾) 1.简单说明 UIKit直接将动画集成到UIView类中,当内部的一些属性发生改变时,UIView将为这些改变提供动画 ...

  8. iOS开发UI篇—核心动画(关键帧动画)

    转自:http://www.cnblogs.com/wendingding/p/3801330.html iOS开发UI篇—核心动画(关键帧动画) 一.简单介绍 是CApropertyAnimatio ...

  9. iOS开发UI篇—核心动画(基础动画)

    转自:http://www.cnblogs.com/wendingding/p/3801157.html 文顶顶 最怕你一生碌碌无为 还安慰自己平凡可贵 iOS开发UI篇—核心动画(基础动画) iOS ...

随机推荐

  1. Eclipse 创建 Java 包

    打开新建 Java 包向导 你可以使用新建 Java 包向导来创建 Java 包.Java 包向导打开方式有: 通过点击 "File" 菜单并选择 New > Package ...

  2. Spring入门之AOP篇

    听了几节IT黑马营的SPRING课程,照着例程写了一个SPRING 中AOP的例子:  一.准备工作 下载复制或配置JAR包.图方便,我将下载的SPRING包都加上去了.另外还需要aspectj的两个 ...

  3. 用ActivatedRoute获取url中的参数

    突然让我用ActivatedRoute import {Injectable} from "@angular/core"; import {ActivatedRoute} from ...

  4. 【翻译自mos文章】当点击完 finishbutton后,dbca 或者dbua hang住

    当点击完 finishbutton后,dbca 或者dbua hang住 来源于: DBCA/DBUA APPEARS TO HANG AFTER CLICKING FINISH BUTTON (文档 ...

  5. boost::lockfree::stack

    #include <boost/thread/thread.hpp> #include <boost/lockfree/stack.hpp> #include <iost ...

  6. 《从零开始学Swift》学习笔记(Day 49)——扩展声明

    原创文章,欢迎转载.转载请注明:关东升的博客 声明扩展的语法格式如下: extension 类型名 { //添加新功能 } 声明扩展的关键字是extension,“类型名”是Swift中已有的类型,包 ...

  7. vertical-align:middle;一般用于img和行内文字对齐方式

    vertical-align:top ;文字和行内块元素的顶部对齐 vertical-align:middle;居中 vertical-align:bottom;底对齐

  8. 通过存储过程运行通过DBLINK的查询语句失败-单个语句成功--ORA-00604

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/q947817003/article/details/24419459 客户遇到个问题,描写叙述例如以 ...

  9. 【深度学习】使用opencv在视频上添加文字和标记框

    深度学习识别出视频的物体之后,需要在视频上画框标记出来. 接下来介绍如何使用python在视频上画框和文字 #!/usr/bin/env python # -*- coding:utf-8 -*- i ...

  10. 1.6 使用电脑测试MC20的读取带中文短信功能

    需要准备的硬件 MC20开发板 1个 https://item.taobao.com/item.htm?id=562661881042 GSM/GPRS天线 1根 https://item.taoba ...