forked from iimgal/StudyiOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimationViewController.m
More file actions
220 lines (195 loc) · 6.44 KB
/
Copy pathAnimationViewController.m
File metadata and controls
220 lines (195 loc) · 6.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
//
// AnimationViewController.m
// StudyiOS
//
// Created by on 11-10-6.
// Copyright (c) 2011年 ZhangYiCheng. All rights reserved.
//
#import "AnimationViewController.h"
#import <QuartzCore/QuartzCore.h>
#define kDuration 0.7 // 动画持续时间(秒)
@implementation AnimationViewController
@synthesize blueView;
@synthesize greenView;
@synthesize typeID;
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
// 增加Code按钮,可跳转至教学页面
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"Code" style:UIBarButtonItemStyleBordered target:self action:@selector(code)];
self.navigationItem.rightBarButtonItem = item;
[super viewDidLoad];
}
- (void)viewDidUnload
{
self.blueView = nil;
self.greenView = nil;
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
#pragma mark -
#pragma mark Core Animation
- (IBAction)buttonPressed1:(id)sender {
UIButton *button = (UIButton *)sender;
NSInteger tag = button.tag;
// 使用Core Animation创建动画
// 创建CATransition对象
CATransition *animation = [CATransition animation];
animation.delegate = self;
// 设定动画时间
animation.duration = kDuration;
// 设定动画快慢(开始与结束时较慢)
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
// 12种类型
switch (tag) {
case 101:
// 设定动画类型
// kCATransitionFade 淡化
// kCATransitionPush 推挤
// kCATransitionReveal 揭开
// kCATransitionMoveIn 覆盖
// @"cube" 立方体
// @"suckEffect" 吸收
// @"oglFlip" 翻转
// @"rippleEffect" 波纹
// @"pageCurl" 翻页
// @"pageUnCurl" 反翻页
// @"cameraIrisHollowOpen" 镜头开
// @"cameraIrisHollowClose" 镜头关
animation.type = kCATransitionFade;
break;
case 102:
animation.type = kCATransitionPush;
break;
case 103:
animation.type = kCATransitionReveal;
break;
case 104:
animation.type = kCATransitionMoveIn;
break;
case 201:
animation.type = @"cube";
break;
case 202:
animation.type = @"suckEffect";
break;
case 203:
animation.type = @"oglFlip";
break;
case 204:
animation.type = @"rippleEffect";
break;
case 205:
animation.type = @"pageCurl";
break;
case 206:
animation.type = @"pageUnCurl";
break;
case 207:
animation.type = @"cameraIrisHollowOpen";
break;
case 208:
animation.type = @"cameraIrisHollowClose";
break;
default:
break;
}
// 四个方向
switch (self.typeID) {
case 0:
// 设定动画方向
animation.subtype = kCATransitionFromLeft;
break;
case 1:
animation.subtype = kCATransitionFromBottom;
break;
case 2:
animation.subtype = kCATransitionFromRight;
break;
case 3:
animation.subtype = kCATransitionFromTop;
break;
default:
break;
}
self.typeID += 1;
if (self.typeID > 3) {
self.typeID = 0;
}
// 视图切换,请替换以下三行代码
NSUInteger green = [[self.view subviews] indexOfObject:self.greenView];
NSUInteger blue = [[self.view subviews] indexOfObject:self.blueView];
[self.view exchangeSubviewAtIndex:green withSubviewAtIndex:blue];
// 动画开始
[[self.view layer] addAnimation:animation forKey:@"animation"];
}
#pragma mark CAAnimationDelegate
// 动画开始时调用
- (void)animationDidStart:(CAAnimation *)anim
{
NSLog(@"animationDidStart");
}
// 动画结束时调用
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
NSLog(@"animationDidStop");
}
#pragma mark -
#pragma mark UIView动画
- (IBAction)buttonPressed2:(id)sender {
UIButton *button = (UIButton *)sender;
NSInteger tag = button.tag;
// 使用UIView创建动画
// 开始设置动画
CGContextRef context = UIGraphicsGetCurrentContext();
// 动画开始
[UIView beginAnimations:nil context:context];
// 设置动画快慢
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
// 设置动画时间
[UIView setAnimationDuration:kDuration];
// 4种类型
switch (tag) {
case 105:
// 设置动画类型
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES];
break;
case 106:
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
break;
case 107:
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
break;
case 108:
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
break;
default:
break;
}
// 视图切换,请替换以下三行代码
NSUInteger green = [[self.view subviews] indexOfObject:self.greenView];
NSUInteger blue = [[self.view subviews] indexOfObject:self.blueView];
[self.view exchangeSubviewAtIndex:green withSubviewAtIndex:blue];
[UIView setAnimationDelegate:self];
// 动画完毕后调用某个方法
//[UIView setAnimationDidStopSelector:@selector(animationFinished:)];
// 提交动画
[UIView commitAnimations];
}
// 跳转至教学页面
- (void)code
{
CodeViewController *controller = [[CodeViewController alloc] initWithNibName:@"CodeViewController" bundle:nil];
NSString *name = [NSString stringWithUTF8String:object_getClassName(self)];
controller.className = name;
[self.navigationController pushViewController:controller animated:YES];
}
@end