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. [HDOJ2818]Building Block(带权并查集,路径压缩)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2818 题意:有N个块,每次有两个操作: M x y表示把x所在的那一堆全部移到y所在的那一堆的下方. ...

  2. SharePoint的安装配置

    安装环境 1. Window server 2008 r2(sp2) OS.2. MS SQL Server 2008 r2.3. Office2010.4. IIS7以上.5. 确认服务器已经加入域 ...

  3. 关于存储过程 output 问题

    在游标循环当中给 output 变量赋值报 指定的转换无效 错误必须在存储过程最后再给 output 变量赋值

  4. php通过curl调用jpush接口实现消息的推送

    public function actionNotifyto() { //$regid = $_REQUEST['regid']; $url = 'https://api.jpush.cn/v3/pu ...

  5. UVA 10801 Lift Hopping 电梯换乘(最短路,变形)

    题意: 有n<6部电梯,给出每部电梯可以停的一些特定的楼层,要求从0层到达第k层出来,每次换乘需要60秒,每部电梯经过每层所耗时不同,具体按 层数*电梯速度 来算.问经过多少秒到达k层(k可以为 ...

  6. 聚焦 SQL 数据库活动异地复制

    Tobias Ternstrom  US-DS-PM 首席部门项目经理 本文作为一系列业务连续性和灾难恢复文章的开篇,概述了业务连续性的各种场景,然后重点介绍 SQL 数据库高级服务级别提供的活动异地 ...

  7. C扩展Python

    基本想法: 先看中文小介绍,再看英文详细文档. 1. 参考 首先参考THIS, IBM的工程师好像出了好多这样的文章啊,而且每次看到时间戳,我都想戳自己- -! 2. ERROR 可能遇到错误: fa ...

  8. 【C#学习笔记】载入图片并居中

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  9. fzu 1675 The Seventy-seven Problem

    给你长度为 10^5~10^6长度,由数字组成的串 其中有4位不见了 补全该串 使得在该串能整除 77的同时 尽可能大// 先计算出每个 n*10^m 模 77 的循环节 n=0,1,2..,9// ...

  10. <五>面向对象分析之UML核心元素之边界

    一:基本概念