A.基本用法
1.CABasicAnimation
  1 //
2 // ViewController.m
3 // CoreAnimationTest
4 //
5 // Created by hellovoidworld on 15/1/14.
6 // Copyright (c) 2015年 hellovoidworld. All rights reserved.
7 //
8
9 #import "ViewController.h"
10
11 @interface ViewController ()
12
13 @property(nonatomic, strong) CALayer *layer;
14
15 @end
16
17 @implementation ViewController
18
19 - (void)viewDidLoad {
20 [super viewDidLoad];
21 // Do any additional setup after loading the view, typically from a nib.
22
23
24 CALayer *layer = [[CALayer alloc] init];
25 layer.bounds = CGRectMake(0, 0, 100, 100);
26 layer.anchorPoint = CGPointZero;
27 layer.position = CGPointMake(100, 200);
28 layer.backgroundColor = [UIColor redColor].CGColor;
29
30 [self.view.layer addSublayer:layer];
31
32 self.layer = layer;
33 }
34
35 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
36 // [self testTransform];
37
38 // [self testRotation];
39
40 // [self testScale];
41
42 [self testTranslate];
43 }
44
45 /** 测试位移转换 */
46 - (void) testTransform {
47 // 1.创建动画对象
48 CABasicAnimation *anim = [CABasicAnimation animation];
49
50 // 2.设置动画
51 anim.duration = 2.0;
52 // 动画设置目标属性
53 anim.keyPath = @"transform.translation.x";
54 // 目标属性值
55 anim.toValue = @(150);
56
57 // 完成后保留动画
58 anim.removedOnCompletion = NO;
59 // 定格动画模式为最后一刻
60 anim.fillMode = kCAFillModeForwards;
61
62 // 3.添加动画到图层
63 [self.layer addAnimation:anim forKey:nil];
64 }
65
66 /** 测试旋转 */
67 - (void) testRotation {
68 // 1.创建动画对象
69 CABasicAnimation *anim = [CABasicAnimation animation];
70
71 // 2.设置动画
72 anim.duration = 2.0;
73
74 // 动画设置目标属性
75 // anim.keyPath = @"transform.rotation";
76 anim.keyPath = @"transform";
77 NSValue *value = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_2, 1, 1, 0)];
78
79 // 目标属性值
80 // anim.toValue = @(M_PI_2);
81 anim.toValue = value;
82
83
84 // 完成后保留动画
85 anim.removedOnCompletion = NO;
86 // 定格动画模式为最后一刻
87 anim.fillMode = kCAFillModeForwards;
88
89 // 3.添加动画到图层
90 [self.layer addAnimation:anim forKey:nil];
91 }
92
93 /** 测试缩放 */
94 - (void) testScale {
95 // 1.创建动画对象
96 CABasicAnimation *anim = [CABasicAnimation animation];
97
98 // 2.设置动画
99 anim.duration = 2.0;
100 // 动画设置目标属性
101 anim.keyPath = @"bounds";
102
103 // 由于属性是bounds,所以x,y属性是无用的,并且要使用CGRect
104 NSValue *fromValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 0, 0)];
105 NSValue *toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 200, 200)];
106
107 // 目标属性值
108 anim.fromValue = fromValue;
109 anim.toValue = toValue;
110
111 // 完成后保留动画
112 anim.removedOnCompletion = NO;
113 // 定格动画模式为最后一刻
114 anim.fillMode = kCAFillModeForwards;
115
116 // 3.添加动画到图层
117 [self.layer addAnimation:anim forKey:nil];
118 }
119
120 - (void) testTranslate {
121 // 1.创建动画对象
122 CABasicAnimation *anim = [CABasicAnimation animation];
123
124 // 2.设置动画
125 anim.duration = 2.0;
126 // 动画设置目标属性
127 anim.keyPath = @"position";
128
129 // 目标属性值
130 NSValue *value = [NSValue valueWithCGPoint:CGPointMake(100, 100)];
131 // 增减目标属性值,所以一直点击就会一直移动下去
132 anim.byValue = value;
133
134 // 完成后保留动画
135 anim.removedOnCompletion = NO;
136 // 定格动画模式为最后一刻
137 anim.fillMode = kCAFillModeForwards;
138
139 // 3.添加动画到图层
140 [self.layer addAnimation:anim forKey:nil];
141 }
142
143
144 - (void)didReceiveMemoryWarning {
145 [super didReceiveMemoryWarning];
146 // Dispose of any resources that can be recreated.
147 }
148
149 @end
 
2.CAKeyframeAnimation
 
  1 //
2 // KeyframeViewController.m
3 // CoreAnimationTest
4 //
5 // Created by hellovoidworld on 15/1/15.
6 // Copyright (c) 2015年 hellovoidworld. All rights reserved.
7 //
8
9 #import "KeyframeViewController.h"
10
11 @interface KeyframeViewController ()
12
13 @property(nonatomic, strong) CALayer *layer;
14
15 @end
16
17 @implementation KeyframeViewController
18
19 - (void)viewDidLoad {
20 [super viewDidLoad];
21 // Do any additional setup after loading the view.
22
23 CALayer *layer = [[CALayer alloc] init];
24 layer.bounds = CGRectMake(0, 0, 100, 100);
25 layer.anchorPoint = CGPointZero;
26 layer.position = CGPointMake(200, 100);
27 layer.backgroundColor = [UIColor redColor].CGColor;
28
29 [self.view.layer addSublayer:layer];
30
31 self.layer = layer;
32 }
33
34 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
35 // [self testPath];
36
37 [self testMutiValue];
38 }
39
40 - (void) testPath {
41 // 创建动画对象
42 CAKeyframeAnimation *anim = [[CAKeyframeAnimation alloc] init];
43
44 // 设置动画
45 anim.keyPath = @"position";
46 anim.removedOnCompletion = NO;
47 anim.fillMode = kCAFillModeForwards;
48 anim.duration = 2.0;
49
50 // 设置绘画路径
51 CGMutablePathRef path = CGPathCreateMutable();
52 // 创建一个圆的轨迹
53 CGPathAddEllipseInRect(path, NULL, CGRectMake(0, 0, 200, 200));
54 // 设置动画轨迹
55 anim.path = path;
56 // 释放路径
57 CGPathRelease(path);
58
59 // 设置动画代理
60 anim.delegate = self;
61
62 [self.layer addAnimation:anim forKey:nil];
63 }
64
65 - (void) testMutiValue {
66 // 创建动画对象
67 CAKeyframeAnimation *anim = [[CAKeyframeAnimation alloc] init];
68
69 // 设置动画
70 anim.keyPath = @"position";
71 anim.removedOnCompletion = NO;
72 anim.fillMode = kCAFillModeForwards;
73 anim.duration = 2.0;
74
75 NSValue *v1 = [NSValue valueWithCGPoint:CGPointMake(0, 0)];
76 NSValue *v2 = [NSValue valueWithCGPoint:CGPointMake(100, 100)];
77 NSValue *v3 = [NSValue valueWithCGPoint:CGPointMake(300, 200)];
78 NSValue *v4 = [NSValue valueWithCGPoint:CGPointMake(120, 50)];
79
80 anim.values = @[v1, v2, v3, v4];
81
82 // 设置动画节奏
83 // 慢进慢出
84 anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
85
86 // 设置动画代理
87 anim.delegate = self;
88
89 [self.layer addAnimation:anim forKey:nil];
90 }
91
92 #pragma mark - 动画代理方法
93 /** 动画开始之后 */
94 - (void)animationDidStart:(CAAnimation *)anim {
95 NSLog(@"animationDidStart");
96 }
97
98 /** 动画结束 */
99 - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
100 NSLog(@"animationDidStop");
101 }
102
103
104 @end
 
3.控件抖动
 
 1 //
2 // ShakeViewController.m
3 // CoreAnimationTest
4 //
5 // Created by hellovoidworld on 15/1/15.
6 // Copyright (c) 2015年 hellovoidworld. All rights reserved.
7 //
8
9 #import "ShakeViewController.h"
10
11 @interface ShakeViewController ()
12
13 @property(nonatomic, strong) UIImageView *imageView;
14
15 @end
16
17 @implementation ShakeViewController
18
19 - (void)viewDidLoad {
20 [super viewDidLoad];
21 // Do any additional setup after loading the view.
22
23 UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"headImage"]];
24 imageView.frame = CGRectMake(100, 100, 100, 100);
25 self.imageView = imageView;
26
27 UIButton *startButton = [UIButton buttonWithType:UIButtonTypeSystem];
28 [startButton setTitle:@"开始" forState:UIControlStateNormal];
29 startButton.frame = CGRectMake(50, 50, 40, 50);
30 [startButton addTarget:self action:@selector(startShake) forControlEvents:UIControlEventTouchUpInside];
31
32 UIButton *stopButton = [UIButton buttonWithType:UIButtonTypeSystem];
33 [stopButton setTitle:@"停止" forState:UIControlStateNormal];
34 stopButton.frame = CGRectMake(150, 50, 40, 50);
35 [stopButton addTarget:self action:@selector(stopShake) forControlEvents:UIControlEventTouchUpInside];
36
37 [self.view addSubview:imageView];
38 [self.view addSubview:startButton];
39 [self.view addSubview:stopButton];
40 }
41
42 /** 开始摆动 */
43 - (void) startShake {
44 NSLog(@"start shake");
45
46 // 创建动画
47 CAKeyframeAnimation *anim = [[CAKeyframeAnimation alloc] init];
48 anim.keyPath = @"transform.rotation";
49 anim.repeatCount = MAXFLOAT;
50 anim.duration = 0.2;
51
52 // 设置摇摆
53 anim.values = @[@(- (M_PI/180 * 5)), @((M_PI/180 * 5)), @(- (M_PI/180 * 5))];
54
55 // 定格动画
56 anim.removedOnCompletion = NO;
57 anim.fillMode = kCAFillModeForwards;
58
59 // 给view加上动画
60 [self.imageView.layer addAnimation:anim forKey:@"shake"];
61 }
62
63 /** 停止摆动 */
64 - (void) stopShake {
65 NSLog(@"stop shake");
66
67 [self.imageView.layer removeAnimationForKey:@"shake"];
68 }
69
70 @end
 
4.过渡效果
 
 1 //
2 // TransitionViewController.m
3 // CoreAnimationTest
4 //
5 // Created by hellovoidworld on 15/1/15.
6 // Copyright (c) 2015年 hellovoidworld. All rights reserved.
7 //
8
9 #import "TransitionViewController.h"
10
11 @interface TransitionViewController ()
12
13 @property(nonatomic, strong) UIImageView *imageView;
14
15 @property(nonatomic, assign) int imageIndex;
16
17 @end
18
19 @implementation TransitionViewController
20
21 - (void)viewDidLoad {
22 [super viewDidLoad];
23 // Do any additional setup after loading the view.
24
25 self.imageIndex = 0;
26 UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1"]];
27 imageView.frame = CGRectMake(80, 50, 160, 240);
28 self.imageView = imageView;
29
30 UIButton *startButton = [UIButton buttonWithType:UIButtonTypeSystem];
31 [startButton setTitle:@"上一张" forState:UIControlStateNormal];
32 startButton.frame = CGRectMake(50, 400, 80, 50);
33 [startButton addTarget:self action:@selector(preImage) forControlEvents:UIControlEventTouchUpInside];
34
35 UIButton *stopButton = [UIButton buttonWithType:UIButtonTypeSystem];
36 [stopButton setTitle:@"下一张" forState:UIControlStateNormal];
37 stopButton.frame = CGRectMake(150, 400, 80, 50);
38 [stopButton addTarget:self action:@selector(nextImage) forControlEvents:UIControlEventTouchUpInside];
39
40 [self.view addSubview:imageView];
41 [self.view addSubview:startButton];
42 [self.view addSubview:stopButton];
43 }
44
45 /** 上一张 */
46 - (void) preImage {
47 NSLog(@"preImage");
48
49 self.imageIndex--;
50 if (self.imageIndex == -1) {
51 self.imageIndex = 8;
52 }
53
54 CATransition *anim = [[CATransition alloc] init];
55 anim.duration = 0.5;
56 anim.type = @"cube";
57 anim.subtype = kCATransitionFromLeft;
58
59 [self.imageView.layer addAnimation:anim forKey:nil];
60
61 [self changeImage:self.imageIndex];
62 }
63
64 /** 下一张 */
65 - (void) nextImage {
66 NSLog(@"nextImage");
67
68 self.imageIndex++;
69 if (self.imageIndex == 9) {
70 self.imageIndex = 0;
71 }
72
73 CATransition *anim = [[CATransition alloc] init];
74 anim.duration = 0.5;
75 anim.type = @"cube";
76 anim.subtype = kCATransitionFromRight;
77
78 [self.imageView.layer addAnimation:anim forKey:nil];
79
80 [self changeImage:self.imageIndex];
81 }
82
83 /** 替换图片 */
84 - (void) changeImage:(int) imageIndex {
85 NSString *imageName = [NSString stringWithFormat:@"%d", self.imageIndex + 1];
86 self.imageView.image = [UIImage imageNamed:imageName];
87 }
88
89 @end
 
5.CAAnimationGroup 动画组(组合多种动画)
 
 1 //
2 // GroupViewController.m
3 // CoreAnimationTest
4 //
5 // Created by hellovoidworld on 15/1/15.
6 // Copyright (c) 2015年 hellovoidworld. All rights reserved.
7 //
8
9 #import "GroupViewController.h"
10
11 @interface GroupViewController ()
12
13 @property(nonatomic, strong) UIView *hvwView;
14
15 @end
16
17 @implementation GroupViewController
18
19 - (void)viewDidLoad {
20 [super viewDidLoad];
21 // Do any additional setup after loading the view.
22
23 UIView *hvwView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
24 hvwView.backgroundColor = [UIColor redColor];
25 self.hvwView = hvwView;
26
27 [self.view addSubview:hvwView];
28 }
29
30 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
31 // 平移
32 CABasicAnimation *anim1 = [[CABasicAnimation alloc] init];
33 anim1.keyPath = @"position";
34 anim1.toValue = [NSValue valueWithCGPoint:CGPointMake(200, 300)];
35
36 // 旋转
37 CABasicAnimation *anim2 = [[CABasicAnimation alloc] init];
38 anim2.keyPath = @"transform.rotation";
39 anim2.toValue = @(M_PI_2);
40
41 // 缩放
42 CABasicAnimation *anim3 = [[CABasicAnimation alloc] init];
43 anim3.keyPath = @"bounds";
44 anim3.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 200, 200)];
45
46 // group动画
47 CAAnimationGroup *animGroup = [[CAAnimationGroup alloc] init];
48 animGroup.animations = @[anim1, anim2, anim3];
49 animGroup.duration = 2.0;
50
51 // 定格动画
52 animGroup.removedOnCompletion = NO;
53 animGroup.fillMode = kCAFillModeForwards;
54
55 [self.hvwView.layer addAnimation:animGroup forKey:nil];
56 }
57
58 @end
 
 

[iOS UI进阶 - 6.2] 核心动画CoreAnimation 练习代码的更多相关文章

  1. [iOS UI进阶 - 6.1] 核心动画CoreAnimation

    A.基本知识 1.概念 Core Animation是一组非常强大的动画处理API,使用它能做出非常炫丽的动画效果,而且往往是事半功倍,使用它需要先添加QuartzCore.framework和引入对 ...

  2. iOS UI进阶-3.0 核心动画

    Core Animation是一组非常强大的动画处理API,使用它能做出非常炫丽的动画效果,而且往往是事半功倍,使用它需要先添加QuartzCore.framework和引入对应的框架<Quar ...

  3. [iOS UI进阶 - 6.3] UIView 动画

    1.UIView转场过渡动画   // // ViewController.m // UIViewAnimationTest // // Created by hellovoidworld on 15 ...

  4. IOS第18天(9,核心动画-动画组)

    ****动画组 // 核心动画都是假象,不能改变layer的真实属性的值// 展示的位置和实际的位置不同.实际位置永远在最开始位置 #import "HMViewController.h&q ...

  5. [iOS UI进阶 - 2.4] 彩票Demo v1.4 转盘动画

    A.需求 幸运广场界面中有一个幸运转盘,平时能够自动缓缓转动 能够选择星座 点击“开始选号”开速旋转转盘,旋转一定周数 转盘转动速度节奏:开始-慢-块-慢-结束 设置其余的背景和按钮   code s ...

  6. [iOS UI进阶 - 0] Quiartz2D

    A.简介 1. 需要掌握的 drawRect:方法的使用 常见图形的绘制:线条.多边形.圆 绘图状态的设置:文字颜色.线宽等 图形上下文状态的保存与恢复 图形上下文栈 1.基本图形绘制* 线段(线宽. ...

  7. [iOS UI进阶 - 6.0] CALayer

    A.基本知识 1.需要掌握的 CALayer的基本属性 CALayer和UIView的关系 position和anchorPoint的作用   2.概念 在iOS中,你能看得见摸得着的东西基本上都是U ...

  8. iOS UI进阶-1.0 Quartz2D

    概述 Quartz 2D是一个二维绘图引擎,同时支持iOS和Mac系统.Quartz 2D能完成的工作: 绘制图形 : 线条\三角形\矩形\圆\弧等 绘制文字 绘制\生成图片(图像) 读取\生成PDF ...

  9. IOS第18天(1,核心动画layer, 旋转,缩放,平移,边框,剪裁,圆角)

    ****动画效果 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [UIView animateWithDurat ...

随机推荐

  1. Java IO 遇到的错误

    1.java.io.FileNotFoundException: /storage/emulated/0/xxx.txt: open failed: EISDIR (Is a directory) 该 ...

  2. [UESTC1059]秋实大哥与小朋友(线段树, 离散化)

    题目链接:http://acm.uestc.edu.cn/#/problem/show/1059 普通线段树+离散化,关键是……离散化后建树和查询都要按照基本法!!!RE了不知道多少次………………我真 ...

  3. hdu 3359 Kind of a Blur (高斯消元 浮点型)

    题目链接 题意: H * W (W,H <= 10) 的矩阵A的某个元素A[i][j],从它出发到其他点的曼哈顿距离小于等于D的所有值的和S[i][j]除上可达点的数目,构成了矩阵B.给定矩阵B ...

  4. POJ 1681 Painter's Problem (高斯消元 枚举自由变元求最小的步数)

    题目链接 题意: 一个n*n 的木板 ,每个格子 都 可以 染成 白色和黄色,( 一旦我们对也个格子染色 ,他的上下左右 都将改变颜色): 给定一个初始状态 , 求将 所有的 格子 染成黄色 最少需要 ...

  5. NSAutoReleasePool

    做iPhone应用开发已经2年多了, 但一些基础的概念性问题只是大致了解, 脑袋中有个模糊的概念. 虽然对平时工作开发没什么影响, 不过时间长了, 心里总是有点虚. 所以从现在开始, 每当我遇到一个模 ...

  6. ASP.NET MVC @helper使用说明

    简单的 @helper 方法应用场景 Razor中的@helper语法让您能够轻松创建可重用的方法,此方法可以在您的视图模板中封装输出功能.他们使代码能更好地重用,也使代码更具有可读性. 在我们定义@ ...

  7. 为 PHP 开发者准备的 12 个调试工具(转)

    为 PHP 开发者准备的 12 个调试工具 PHP是在实践中发展迅速并被最多使用的脚本语言:包含了诸如详细的文档.庞大的社区.无数可使用的脚本及支持框架等许多特性.PHP提供的这些特性使得它比Pyth ...

  8. Mac终端编译运行C++

    1.在编辑器中写好C++代码 2.打开终端打开文件对应的地址 3.用g++命令来编译.cpp文件 4.用./文件名来运行 观察文件的目录可发现 g++ 源文件名 编译源文件,产生a.out ./文件名 ...

  9. Spring各jar包的作用(转载)

    spring.jar是包含有完整发布的单个jar 包,spring.jar中包含除了spring-mock.jar里所包含的内容外其它所有jar包的内容,因为只有在开发环境下才会用到 spring-m ...

  10. HTML5实现扫描识别二维码/生成二维码

    扫描识别二维码 思路: 1. 操作摄像头,获取图片.HTML5 WEBRTC的navigator.getUserMedia方法去实时获取摄像头资源.  2. 利用canvas使用相关算法分析图片识别图 ...