[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 ...
随机推荐
- 1450. Russian Pipelines(spfa)
1450 水题 最长路 #include <iostream> #include<cstdio> #include<cstring> #include<alg ...
- 函数 page_dir_get_n_heap
查看某page中含有的记录个数 #define PAGE_N_HEAP 4 /* number of records in the heap, bit =flag: new-style compact ...
- struts2中改变struts.xml默认路径
struts2.X配置文件默认存放路径在/WEB-INF/classes目录下,即将struts.xml放在src的目录下. 但是为了方便管理,开发人员把struts.xml放到其他位置,处理方法如下 ...
- 五大主流SQL数据库
一. 开放性 1. SQL Server 只能在windows上运行,没有丝毫的开放性,操作系统的系统的稳定对数据库是十分重要的.Windows9X系列产品是偏重于桌面应用,NT server只适合中 ...
- hdu 4674 Trip Advisor(缩点+倍增lca)
花了一天半的时间,才把这道题ac= = 确实是道好题,好久没敲这么长的code了,尤其是最后的判定,各种销魂啊~ 题目中给出的条件最值得关注的就是:每个点最多只能在一个环内->原图是由一个个边连 ...
- 浅析Java中HashMap的实现
概述 HashMap是一个散列表,是基于拉链法实现的.这个类继承了Map接口,Map接口提供了所有的哈希操作,比如set().put().remove()等,并且允许操作的键值对为null.HashM ...
- plsql developer 使用技巧
plsql developer 使用技巧 Oracle数据库相信已成为很多企业构建自身关键业务的重要后台支撑,也是众多开发人员常常采用的后台.但Oracle自己提供的一套客户端工具不是很好用,极大的影 ...
- java 异常java.lang.UnsupportedOperationException
在项目中采用一个枚举的集合,本人采用Collections中的空集合Collections.emptyList()在添加时发生异常: 常见集合如下: private List<VacationC ...
- SSH整合所需jar
Struts2.1.8+Hibernate3.2+Spring4.1.6+MySql ant-1.6.5.jar ant-antlr-1.6.5.jar ant-junit-1.6.5.jar ant ...
- 项目管理工具:Maven使用方法总结
阅读目录 一.概念 二.Maven安装 三.常用命令 四.生命周期 五.第一个Maven项目 六.POM文件 七.Maven库 八.参考资料 回到顶部 一.概念 Maven是一个项目管理和构建自动化工 ...