-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMasterViewController.m
More file actions
executable file
·236 lines (203 loc) · 10.4 KB
/
MasterViewController.m
File metadata and controls
executable file
·236 lines (203 loc) · 10.4 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
//
// MasterViewController.m
// ShapeAnimation_iOSDemo
//
// Created by Zhang Yungui on 15/2/24.
// Copyright (c) 2015 github.com/rhcad. All rights reserved.
//
#import "MasterViewController.h"
#import "DetailViewController.h"
#import "ShapeAnimation.h"
@implementation MasterViewController
- (void)awakeFromNib {
[super awakeFromNib];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
self.clearsSelectionOnViewWillAppear = NO;
self.preferredContentSize = CGSizeMake(320, 600.0);
}
}
#pragma mark - Segues
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
DetailViewController *controller = (DetailViewController *)[segue destinationViewController];
if ([segue.identifier isEqualToString:@"Add Lines"]) {
controller.animationBlock = self.testAddLines;
} else if ([segue.identifier isEqualToString:@"Move Lines"]) {
controller.animationBlock = self.testMoveLines;
} else if ([segue.identifier isEqualToString:@"Rotate Polygons"]) {
controller.animationBlock = self.testRotatePolygons;
} else if ([segue.identifier isEqualToString:@"Radar Circles"]) {
controller.animationBlock = self.testRadarCircles;
} else if ([segue.identifier isEqualToString:@"Jumping Ball"]) {
controller.animationBlock = self.testJumpingBall;
}
controller.title = NSLocalizedString(segue.identifier, nil);
}
// Demo about strokeEndAnimation, lineWidthAnimation, tapAnimation, shakeAnimation and addSelectionBorders.
- (AnimationBlock)testAddLines {
return ^(SAShapeView *view) {
view.lineWidth = 7;
CAShapeLayer *a = [view addShapeLayer:CGPathFromSVGPath(@"M10,20L150,30 120,200Z") origin:CGPointMake(10, 10)];
CAShapeLayer *b = [view addShapeLayer:CGPathFromSVGPath(@"M10,20L150,30 120,200 ") origin:CGPointMake(80, 30)];
CAShapeLayer *c = [view addShapeLayer:CGPathFromSVGPath(@"M120,200L150,30 10,20Z") origin:CGPointMake(150, 50)];
[[CAAnimationGroup group:@[a.strokeEndAnimation, [a lineWidthAnimation:0 to:5]]] apply:^{
[[a shakeAnimation] apply];
[[c flashAnimation:2] apply];
}];
a.strokeColor = UIColor.redColor.CGColor;
b.strokeColor = UIColor.purpleColor.CGColor;
c.strokeColor = UIColor.greenColor.CGColor;
// Test hit-testing and dragging
view.didTap = ^(SAShapeView *view, CGPoint point) {
[view removeSelectionBorders];
CALayer *layer = [view hitTestLayer:point];
if (layer) {
[layer.tapAnimation apply:^{
[view addSelectionBorder:layer];
}];
}
};
__block NSArray *selectedLayers = nil;
view.didPan = ^(SAShapeView *view, SAPanRecognizer *sender) {
if (sender.state == SAGestureBegan) {
selectedLayers = view.selectedLayers;
[view removeSelectionBorders];
}
else if (sender.state == SAGestureChanged) {
CGPoint translation = [sender translationInView:view];
[sender setTranslation:CGPointZero inView:view];
for (CALayer *layer in selectedLayers) {
[CAAnimation suppressAnimation:^{
layer.position = SAPointAdd(layer.position, translation);
}];
}
}
else if (sender.state == SAGestureEnded) {
[view addSelectionBorders:selectedLayers];
selectedLayers = nil;
}
};
};
}
// Demo about didTap, moveOnPathAnimation, moveAnimation, rotationAnimation, dashPhaseAnimation and animationGroup.
// Rotate and move a picture and polygon with gradient fill along the path.
- (AnimationBlock)testMoveLines {
return ^(SAShapeView *view) {
CGPathRef path = CGPathFromSVGPath(@"M30,20 C0,50 50,175 130,80 T250,100");
// Add a triangle with gradient fill
CAShapeLayer *la1 = [view addShapeLayer:CGPathFromSVGPath(@"M10,20L50,40 20,80Z")];
la1.gradient = [SAGradient gradient:@[(id)CGColorFromRGBA(0.5, 0.5, 0.9, 1.0),
(id)CGColorFromRGBA(0.9, 0.9, 0.3, 1.0)]];
// Move and rotate the triangle along the path
SAAnimationPair *a1 = [[la1 moveOnPathAnimation:path] setDuration:1.6];
SAAnimationPair *a2 = [la1.rotate360Degrees forever];
[[CAAnimationGroup group:@[a1, a2]].autoreverses.forever apply];
// Show the path with animated color
CAShapeLayer *guild = [view addShapeLayer:path];
[[guild strokeColorAnimation:SAColor.blueColor.CGColor
to:SAColor.yellowColor.CGColor].autoreverses.forever apply];
// Rotate and move a picture along the path
CALayer *imageLayer = [view addImageLayer:CGPointMake(200, 200) named:@"airship.png"];
imageLayer.opacity = 0.7f;
[[[imageLayer moveOnPathAnimation:path autoRotate:YES] setBeginTime:1.0] applyWithDuration:4];
CALayer *borderLayer = [view addSelectionBorder:imageLayer];
[[[borderLayer moveOnPathAnimation:path autoRotate:YES]
setBeginTime:1.0] applyWithDuration:4 didStop:^{
[borderLayer removeLayer];
[[imageLayer scaleAnimation:0] apply:^{
[imageLayer removeLayer];
}];
}];
// Test tapping
view.didTap = ^(SAShapeView *view, CGPoint point) {
CALayer *layer = [view hitTestLayer:point];
if (layer.didTap) {
layer.didTap(layer);
} else {
layer.paused = !layer.paused;
}
};
imageLayer.didTap = ^(CALayer *layer) { [layer.rotate360Degrees apply]; };
};
}
// Demo about polygon with text and gradient fill moving and rotating one by one.
// Modified from http://zulko.github.io/blog/2014/09/20/vector-animations-with-python/
- (AnimationBlock)testRotatePolygons {
return ^(SAShapeView *view) {
CGPathRef path = [SAPath pathWithRegularPolygon:5 center:CGPointMake(25, 25) radius:25 startAngle:0].CGPath;
CGRect bounds = CGRectMake(0, 0, 50, 50);
NSMutableArray *animations = NSMutableArray.array;
NSArray *chars = @[@"T", @"E", @"S", @"T"];
SAGradient *gradient = [SAGradient gradient:@[(id)CGColorFromRGBA(0, 0.5, 1, 1),
(id)CGColorFromRGBA(0, 1, 1, 1)]];
gradient.startPoint = CGPointZero;
gradient.endPoint = CGPointMake(1, 1);
for (int i = 0; i < chars.count; i++) {
CGPoint pt = CGPointMake(50 + 60*i, 50);
CAShapeLayer *edge = [view addShapeLayer:path bounds:bounds center:pt];
edge.gradient = gradient;
CALayer *text = [view addTextLayer:[chars objectAtIndex:i] center:pt fontSize:30];
[animations addObject:[[edge rotationAnimation:(5-i)*M_PI_2] setBeginTime:i gap:0.3 duration:1.5]];
[animations addObject:[[text rotationAnimation:(5-i)*M_PI_2] setBeginTime:i gap:0.3 duration:1.5]];
}
[CATransaction apply:animations completion:^{
for (int i = 0; i < animations.count; i++) {
SAAnimationPair *pair = [animations objectAtIndex:i];
[[[pair.layer moveAnimation:CGPointZero to:CGPointMake(300, 0) relative:YES]
setBeginTime:5 - i/2 gap:0.3] apply];
}
}];
};
}
// Demo about growing circles.
- (AnimationBlock)testRadarCircles {
return ^(SAShapeView *view) {
int count = 8;
float duration = 2;
for (int i = 0; i < count; i++) {
CAShapeLayer *a = [view addCircleLayer:CGPointMake(100, 100) radius:15];
SAAnimationPair *p = [CAAnimationGroup group:@[[a scaleAnimation:0 to:5], [a opacityAnimation:1 to:0]]];
[p setBeginTime:i gap:(duration / count) duration:duration];
[[p.forever setFillMode:kCAFillModeBackwards] apply];
}
for (int i = 0; i < count; i++) {
CAShapeLayer *a = [view addCircleLayer:CGPointMake(200, 100) radius:0.1];
CGPathRef path = [SAPath pathWithCircle:CGPointZero radius:75].CGPath;
SAAnimationPair *p = [CAAnimationGroup group:@[[a switchPathAnimation:path], [a opacityAnimation:1 to:0]]];
[p setBeginTime:i gap:(duration / count) duration:duration];
[[p.forever setFillMode:kCAFillModeBackwards] apply];
}
};
}
// Demo about jumping ball with shadow.
// Modified from http://zulko.github.io/blog/2014/09/20/vector-animations-with-python/
- (AnimationBlock)testJumpingBall {
return ^(SAShapeView *view) {
SAGradient *gradient = [SAGradient gradient:@[(id)CGColorFromRGBA(1,0,0,1),
(id)CGColorFromRGBA(0.1,0,0,1)] axial:YES];
gradient.startPoint = CGPointMake(0.3, 0.3);
SAAnimationLayer *layer = [view addAnimationLayer:@[@"t", @0]];
layer.drawBlock = ^(SAAnimationLayer *layer, CGContextRef ctx) {
CGFloat t = [layer getProperty:@"t"];
CGFloat w = layer.bounds.size.width, h = layer.bounds.size.height;
CGFloat d = 3, r = 30; // radius of ball
CGFloat dj = w / 6, hj = h / 2; // distance and height of jumping
CGFloat ground = 0.75f * h;
CGFloat x = (-w / 3) + (5 * w / 3) * (t / d);
CGFloat xdj = fmodf(x, dj);
CGFloat y = ground - hj * 4 * xdj * (dj - xdj) / (dj * dj);
CGFloat coef = (hj - y) / hj;
CGFloat sr = (1 - coef / 4) * r;
CGRect shadow = CGRectMake(x - sr, ground + r/2 - sr/2, sr*2, sr);
SAGradient *sgradient = [SAGradient gradient:@[(id)[UIColor colorWithWhite:0 alpha:0.2-coef/5].CGColor,
(id)UIColor.clearColor.CGColor] axial:YES];
[sgradient fillEllipseInContext:ctx rect:shadow];
CGRect ball = CGRectMake(x - r, y - r, r*2, r*2);
[gradient fillEllipseInContext:ctx rect:ball];
};
layer.animationCreated = ^(SAAnimationLayer *layer, CABasicAnimation *anim) {
anim.repeatCount = HUGE; anim.duration = 10;
};
[layer setProperty:4 key:@"t"];
};
}
@end