[iOS UI进阶 - 6.2] 核心动画CoreAnimation 练习代码
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

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

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

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

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 练习代码的更多相关文章
- [iOS UI进阶 - 6.1] 核心动画CoreAnimation
A.基本知识 1.概念 Core Animation是一组非常强大的动画处理API,使用它能做出非常炫丽的动画效果,而且往往是事半功倍,使用它需要先添加QuartzCore.framework和引入对 ...
- iOS UI进阶-3.0 核心动画
Core Animation是一组非常强大的动画处理API,使用它能做出非常炫丽的动画效果,而且往往是事半功倍,使用它需要先添加QuartzCore.framework和引入对应的框架<Quar ...
- [iOS UI进阶 - 6.3] UIView 动画
1.UIView转场过渡动画 // // ViewController.m // UIViewAnimationTest // // Created by hellovoidworld on 15 ...
- IOS第18天(9,核心动画-动画组)
****动画组 // 核心动画都是假象,不能改变layer的真实属性的值// 展示的位置和实际的位置不同.实际位置永远在最开始位置 #import "HMViewController.h&q ...
- [iOS UI进阶 - 2.4] 彩票Demo v1.4 转盘动画
A.需求 幸运广场界面中有一个幸运转盘,平时能够自动缓缓转动 能够选择星座 点击“开始选号”开速旋转转盘,旋转一定周数 转盘转动速度节奏:开始-慢-块-慢-结束 设置其余的背景和按钮 code s ...
- [iOS UI进阶 - 0] Quiartz2D
A.简介 1. 需要掌握的 drawRect:方法的使用 常见图形的绘制:线条.多边形.圆 绘图状态的设置:文字颜色.线宽等 图形上下文状态的保存与恢复 图形上下文栈 1.基本图形绘制* 线段(线宽. ...
- [iOS UI进阶 - 6.0] CALayer
A.基本知识 1.需要掌握的 CALayer的基本属性 CALayer和UIView的关系 position和anchorPoint的作用 2.概念 在iOS中,你能看得见摸得着的东西基本上都是U ...
- iOS UI进阶-1.0 Quartz2D
概述 Quartz 2D是一个二维绘图引擎,同时支持iOS和Mac系统.Quartz 2D能完成的工作: 绘制图形 : 线条\三角形\矩形\圆\弧等 绘制文字 绘制\生成图片(图像) 读取\生成PDF ...
- IOS第18天(1,核心动画layer, 旋转,缩放,平移,边框,剪裁,圆角)
****动画效果 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [UIView animateWithDurat ...
随机推荐
- SQLserver游标原理和使用方法
在数据库开发过程中,当你检索的数据只是一条记录时,你所编写的事务语句代码往往使用SELECT INSERT 语句.但是我们常常会遇到这样情况,即从某一结果集中逐一地读取一条记录.那么如何解决这种问题呢 ...
- basicjava
.完数 . 第一个完全数是6,它有约数1.2.3.6,除去它本身6外,其余3个数相加,1+2+3=6.第二个完全数是28,它有约数1.2.4.7.14. 28,除去它本身28外,其余5个数相加,1+2 ...
- 使用Quartz创建定时任务
项目开发中经常需要定时循环执行某些任务 比如定时发送报表,定时发送邮件,亦或者定时清理缓存,定时更新数据等等 有些时候可以简单地利用Windows Server的计划任务执行程序 Linux也有相应的 ...
- POJ 1276 (多重背包) Cash Machine
题意: 有n种纸币,已知每种纸币的面值和数量,求所能凑成的不超过cash的最大总面值. 分析: 这道题自己写了一下TLE了,好可耻.. 找了份比较简洁的代码抄过来了..poj1276 #include ...
- mysql 分页存储过程 一次返回两个记录集(行的条数,以及行记录),DataReader的Read方法和NextResult方法
DELIMITER $$ USE `netschool`$$ DROP PROCEDURE IF EXISTS `fn_jk_GetCourses`$$ CREATE DEFINER=`root`@` ...
- 使用mp4v2将H264+AAC合成mp4文件
录制程序要添加新功能:录制CMMB电视节目,我们的板卡发送出来的是RTP流(H264视频和AAC音频),录制程序要做的工作是: (1)接收并解析RTP包,分离出H264和AAC数据流: (2)将H26 ...
- ORACLE 如何定位消耗资源的SQL
在分析SQL性能的时候,经常需要确定资源消耗多的SQL,总结如下: 1 查看值得怀疑的SQLselect substr(to_char(s.pct, '99.00'), 2) || '%' load, ...
- 【MySQL for Mac】在Mac终端导入&导出.sql文件
导入 打开终端输入:(前提是已经配置过MySQL环境变量) mysql -u root -p create database name; use name; source 『将.sql文件直接拖拽至终 ...
- Oracle“死锁”模拟
本着实验优先的原则,先模拟死锁的发生,然后在列一下死锁产生的四个必要条件和处理死锁的一般策略. 1.创建两个简单的表t1_deadlock和t2_deadlock,每个表中仅仅包含一个字段asys@o ...
- 淘宝语音搜索的实现——html5
作为一个专业的淘宝控,不知道从什么时候开始发现淘宝上居然还有语音搜索,好吧,因为好奇心作祟还是想一探究竟.不过我想仔细一点的人,都会发现在只有在webkit内核的浏览器上有,原因是它只支持webkit ...