This repository has been archived by the owner on May 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
SKBounceAnimation.m
384 lines (319 loc) · 14.4 KB
/
SKBounceAnimation.m
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
//
// SKBounceAnimation.m
// SKBounceAnimation
//
// Created by Soroush Khanlou on 6/15/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "SKBounceAnimation.h"
/*
Keypaths:
Float animations:
anchorPoint
cornerRadius
borderWidth
opacity
shadowRadius
shadowOpacity
zPosition
Point/size animations:
position
shadowOffset
Rect animations:
bounds
frame - not strictly animatable, use bounds
contentsRect
Colors:
backgroundColor
borderColor
shadowColor
CATransform3D:
transform
Meaningless:
backgroundFilters
compositingFilter
contents
doubleSided
filters
hidden
mask
masksToBounds
sublayers
sublayerTransform
*/
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-property-ivar"
SKBounceAnimationStiffness SKBounceAnimationStiffnessLight = 5.f;
SKBounceAnimationStiffness SKBounceAnimationStiffnessMedium = .1f;
SKBounceAnimationStiffness SKBounceAnimationStiffnessHeavy = .001f;
@interface SKBounceAnimation (Private)
- (void) createValueArray;
- (NSArray*) valueArrayForStartValue:(CGFloat)startValue endValue:(CGFloat)endValue;
- (CGPathRef) createPathFromXValues:(NSArray*)xValues yValues:(NSArray*)yValues;
- (NSArray*) createRectArrayFromXValues:(NSArray*)xValues yValues:(NSArray*)yValues widths:(NSArray*)widths heights:(NSArray*)heights;
- (NSArray*) createColorArrayFromRed:(NSArray*)redValues green:(NSArray*)greenValues blue:(NSArray*)blueValues alpha:(NSArray*)alphaValues;
- (NSArray*) createTransformArrayFromM11:(NSArray*)m11 M12:(NSArray*)m12 M13:(NSArray*)m13 M14:(NSArray*)m14
M21:(NSArray*)m21 M22:(NSArray*)m22 M23:(NSArray*)m23 M14:(NSArray*)m24
M31:(NSArray*)m31 M32:(NSArray*)m32 M33:(NSArray*)m33 M14:(NSArray*)m34
M41:(NSArray*)m41 M42:(NSArray*)m42 M43:(NSArray*)m43 M14:(NSArray*)m44;
@end
@implementation SKBounceAnimation
@synthesize fromValue, byValue, toValue, numberOfBounces, shouldOvershoot;
+ (SKBounceAnimation*) animationWithKeyPath:(NSString*)keyPath {
return [[self alloc] initWithKeyPath:keyPath];
}
- (id) initWithKeyPath:(NSString*)keyPath {
self = [super init];
if (self) {
super.keyPath = keyPath;
self.numberOfBounces = 2;
self.shouldOvershoot = YES;
self.stiffness = SKBounceAnimationStiffnessMedium;
}
return self;
}
- (void) setFromValue:(id)newFromValue {
[super setValue:newFromValue forKey:@"fromValueKey"];
[self createValueArray];
}
- (id) fromValue {
return [super valueForKey:@"fromValueKey"];
}
- (void) setToValue:(id)newToValue {
[super setValue:newToValue forKey:@"toValueKey"];
[self createValueArray];
}
- (id) toValue {
return [super valueForKey:@"toValueKey"];
}
- (void) setDuration:(CFTimeInterval)newDuration {
[super setDuration:newDuration];
[self createValueArray];
}
- (void) setNumberOfBounces:(NSUInteger)newNumberOfBounces {
[super setValue:[NSNumber numberWithUnsignedInteger:newNumberOfBounces] forKey:@"numberOfBouncesKey"];
[self createValueArray];
}
- (NSUInteger) numberOfBounces {
return [[super valueForKey:@"numberOfBouncesKey"] unsignedIntValue];
}
- (void) setStiffness:(SKBounceAnimationStiffness)stiffness {
[super setValue:@(stiffness) forKey:@"stifnessKey"];
[self createValueArray];
}
- (SKBounceAnimationStiffness) stiffness {
return [[super valueForKey:@"stifnessKey"] floatValue];
}
- (void) setShouldOvershoot:(BOOL)newShouldOvershoot {
[super setValue:[NSNumber numberWithBool:newShouldOvershoot] forKey:@"shouldOvershootKey"];
[self createValueArray];
}
- (BOOL) shouldOvershoot {
return [[super valueForKey:@"shouldOvershootKey"] boolValue];
}
- (void) setShake:(BOOL)newShake {
[super setValue:[NSNumber numberWithBool:newShake] forKey:@"shakeKey"];
[self createValueArray];
}
- (BOOL) shake {
return [[super valueForKey:@"shakeKey"] boolValue];
}
- (void) createValueArray {
if (self.fromValue && self.toValue && self.duration) {
if ([self.fromValue isKindOfClass:[NSNumber class]] && [self.toValue isKindOfClass:[NSNumber class]]) {
self.values = [self valueArrayForStartValue:[self.fromValue floatValue] endValue:[self.toValue floatValue]];
} else if ([self.fromValue isKindOfClass:[UIColor class]] && [self.toValue isKindOfClass:[UIColor class]]) {
const CGFloat *fromComponents = CGColorGetComponents(((UIColor*)self.fromValue).CGColor);
const CGFloat *toComponents = CGColorGetComponents(((UIColor*)self.toValue).CGColor);
// NSLog(@"from %0.2f %0.2f %0.2f %0.2f", fromComponents[0], fromComponents[1], fromComponents[2], fromComponents[3]);
// NSLog(@"to %0.2f %0.2f %0.2f %0.2f", toComponents[0], toComponents[1], toComponents[2], toComponents[3]);
self.values = [self createColorArrayFromRed:
[self valueArrayForStartValue:fromComponents[0] endValue:toComponents[0]]
green:
[self valueArrayForStartValue:fromComponents[1] endValue:toComponents[1]]
blue:
[self valueArrayForStartValue:fromComponents[2] endValue:toComponents[2]]
alpha:
[self valueArrayForStartValue:fromComponents[3] endValue:toComponents[3]]];
} else if ([self.fromValue isKindOfClass:[NSValue class]] && [self.toValue isKindOfClass:[NSValue class]]) {
NSString *valueType = [NSString stringWithCString:[self.fromValue objCType] encoding:NSStringEncodingConversionAllowLossy];
if ([valueType rangeOfString:@"CGRect"].location == 1) {
CGRect fromRect = [self.fromValue CGRectValue];
CGRect toRect = [self.toValue CGRectValue];
self.values = [self createRectArrayFromXValues:
[self valueArrayForStartValue:fromRect.origin.x endValue:toRect.origin.x]
yValues:
[self valueArrayForStartValue:fromRect.origin.y endValue:toRect.origin.y]
widths:
[self valueArrayForStartValue:fromRect.size.width endValue:toRect.size.width]
heights:
[self valueArrayForStartValue:fromRect.size.height endValue:toRect.size.height]];
} else if ([valueType rangeOfString:@"CGPoint"].location == 1) {
CGPoint fromPoint = [self.fromValue CGPointValue];
CGPoint toPoint = [self.toValue CGPointValue];
CGPathRef path = createPathFromXYValues([self valueArrayForStartValue:fromPoint.x endValue:toPoint.x], [self valueArrayForStartValue:fromPoint.y endValue:toPoint.y]);
self.path = path;
CGPathRelease(path);
} else if ([valueType rangeOfString:@"CATransform3D"].location == 1) {
CATransform3D fromTransform = [self.fromValue CATransform3DValue];
CATransform3D toTransform = [self.toValue CATransform3DValue];
// NSLog(@"from [%0.2f %0.2f %0.2f %0.2f; %0.2f %0.2f %0.2f %0.2f; %0.2f %0.2f %0.2f %0.2f; %0.2f %0.2f %0.2f %0.2f]", fromTransform.m11, fromTransform.m12, fromTransform.m13, fromTransform.m14, fromTransform.m21, fromTransform.m22, fromTransform.m23, fromTransform.m24, fromTransform.m31, fromTransform.m32, fromTransform.m33, fromTransform.m34, fromTransform.m41, fromTransform.m42, fromTransform.m43, fromTransform.m44);
//
// NSLog(@"to [%0.2f %0.2f %0.2f %0.2f; %0.2f %0.2f %0.2f %0.2f; %0.2f %0.2f %0.2f %0.2f; %0.2f %0.2f %0.2f %0.2f]", toTransform.m11, toTransform.m12, toTransform.m13, toTransform.m14, toTransform.m21, toTransform.m22, toTransform.m23, toTransform.m24, toTransform.m31, toTransform.m32, toTransform.m33, toTransform.m34, toTransform.m41, toTransform.m42, toTransform.m43, toTransform.m44);
self.values = [self createTransformArrayFromM11:
[self valueArrayForStartValue:fromTransform.m11 endValue:toTransform.m11]
M12:
[self valueArrayForStartValue:fromTransform.m12 endValue:toTransform.m12]
M13:
[self valueArrayForStartValue:fromTransform.m13 endValue:toTransform.m13]
M14:
[self valueArrayForStartValue:fromTransform.m14 endValue:toTransform.m14]
M21:
[self valueArrayForStartValue:fromTransform.m21 endValue:toTransform.m21]
M22:
[self valueArrayForStartValue:fromTransform.m22 endValue:toTransform.m22]
M23:
[self valueArrayForStartValue:fromTransform.m23 endValue:toTransform.m23]
M24:
[self valueArrayForStartValue:fromTransform.m24 endValue:toTransform.m24]
M31:
[self valueArrayForStartValue:fromTransform.m31 endValue:toTransform.m31]
M32:
[self valueArrayForStartValue:fromTransform.m32 endValue:toTransform.m32]
M33:
[self valueArrayForStartValue:fromTransform.m33 endValue:toTransform.m33]
M34:
[self valueArrayForStartValue:fromTransform.m34 endValue:toTransform.m34]
M41:
[self valueArrayForStartValue:fromTransform.m41 endValue:toTransform.m41]
M42:
[self valueArrayForStartValue:fromTransform.m42 endValue:toTransform.m42]
M43:
[self valueArrayForStartValue:fromTransform.m43 endValue:toTransform.m43]
M44:
[self valueArrayForStartValue:fromTransform.m44 endValue:toTransform.m44]
];
} else if ([valueType rangeOfString:@"CGSize"].location == 1) {
CGSize fromSize = [self.fromValue CGSizeValue];
CGSize toSize = [self.toValue CGSizeValue];
CGPathRef path = createPathFromXYValues(
[self valueArrayForStartValue:fromSize.width endValue:toSize.width],
[self valueArrayForStartValue:fromSize.height endValue:toSize.height]
);
self.path = path;
CGPathRelease(path);
}
}
self.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
}
}
- (NSArray*) createRectArrayFromXValues:(NSArray*)xValues yValues:(NSArray*)yValues widths:(NSArray*)widths heights:(NSArray*)heights {
NSAssert(xValues.count == yValues.count && xValues.count == widths.count && xValues.count == heights.count, @"array must have arrays of equal size");
NSUInteger numberOfRects = xValues.count;
NSMutableArray *values = [NSMutableArray arrayWithCapacity:numberOfRects];
CGRect value;
for (NSInteger i = 1; i < numberOfRects; i++) {
value = CGRectMake(
[[xValues objectAtIndex:i] floatValue],
[[yValues objectAtIndex:i] floatValue],
[[widths objectAtIndex:i] floatValue],
[[heights objectAtIndex:i] floatValue]
);
[values addObject:[NSValue valueWithCGRect:value]];
}
return values;
}
static CGPathRef createPathFromXYValues(NSArray *xValues, NSArray *yValues) {
NSUInteger numberOfPoints = xValues.count;
CGMutablePathRef path = CGPathCreateMutable();
CGPoint value;
value = CGPointMake([[xValues objectAtIndex:0] floatValue], [[yValues objectAtIndex:0] floatValue]);
CGPathMoveToPoint(path, NULL, value.x, value.y);
for (NSInteger i = 1; i < numberOfPoints; i++) {
value = CGPointMake([[xValues objectAtIndex:i] floatValue], [[yValues objectAtIndex:i] floatValue]);
CGPathAddLineToPoint(path, NULL, value.x, value.y);
}
return path;
}
- (NSArray*) createTransformArrayFromM11:(NSArray*)m11 M12:(NSArray*)m12 M13:(NSArray*)m13 M14:(NSArray*)m14
M21:(NSArray*)m21 M22:(NSArray*)m22 M23:(NSArray*)m23 M24:(NSArray*)m24
M31:(NSArray*)m31 M32:(NSArray*)m32 M33:(NSArray*)m33 M34:(NSArray*)m34
M41:(NSArray*)m41 M42:(NSArray*)m42 M43:(NSArray*)m43 M44:(NSArray*)m44 {
NSUInteger numberOfTransforms = m11.count;
NSMutableArray *values = [NSMutableArray arrayWithCapacity:numberOfTransforms];
CATransform3D value;
for (NSInteger i = 1; i < numberOfTransforms; i++) {
value = CATransform3DIdentity;
value.m11 = [[m11 objectAtIndex:i] floatValue];
value.m12 = [[m12 objectAtIndex:i] floatValue];
value.m13 = [[m13 objectAtIndex:i] floatValue];
value.m14 = [[m14 objectAtIndex:i] floatValue];
value.m21 = [[m21 objectAtIndex:i] floatValue];
value.m22 = [[m22 objectAtIndex:i] floatValue];
value.m23 = [[m23 objectAtIndex:i] floatValue];
value.m24 = [[m24 objectAtIndex:i] floatValue];
value.m31 = [[m31 objectAtIndex:i] floatValue];
value.m32 = [[m32 objectAtIndex:i] floatValue];
value.m33 = [[m33 objectAtIndex:i] floatValue];
value.m44 = [[m34 objectAtIndex:i] floatValue];
value.m41 = [[m41 objectAtIndex:i] floatValue];
value.m42 = [[m42 objectAtIndex:i] floatValue];
value.m43 = [[m43 objectAtIndex:i] floatValue];
value.m44 = [[m44 objectAtIndex:i] floatValue];
[values addObject:[NSValue valueWithCATransform3D:value]];
}
return values;
}
- (NSArray*) createColorArrayFromRed:(NSArray*)redValues green:(NSArray*)greenValues blue:(NSArray*)blueValues alpha:(NSArray*)alphaValues {
NSAssert(redValues.count == blueValues.count && redValues.count == greenValues.count && redValues.count == alphaValues.count, @"array must have arrays of equal size");
NSUInteger numberOfColors = redValues.count;
NSMutableArray *values = [NSMutableArray arrayWithCapacity:numberOfColors];
UIColor *value;
for (NSInteger i = 1; i < numberOfColors; i++) {
value = [UIColor colorWithRed:[[redValues objectAtIndex:i] floatValue]
green:[[greenValues objectAtIndex:i] floatValue]
blue:[[blueValues objectAtIndex:i] floatValue]
alpha:[[alphaValues objectAtIndex:i] floatValue]];
[values addObject:(id)value.CGColor];
}
return values;
}
- (NSArray*) valueArrayForStartValue:(CGFloat)startValue endValue:(CGFloat)endValue {
NSInteger steps = 60*self.duration; //60 fps desired
CGFloat stiffnessCoefficient = self.stiffness;
CGFloat alpha = 0;
if (startValue == endValue) {
alpha = log2f(stiffnessCoefficient)/steps;
} else {
alpha = log2f(stiffnessCoefficient/fabs(endValue - startValue))/steps;
}
if (alpha > 0) {
alpha = -1.0f*alpha;
}
CGFloat numberOfPeriods = self.numberOfBounces/2 + 0.5;
CGFloat omega = numberOfPeriods * 2*M_PI/steps;
//uncomment this to get the equation of motion
// NSLog(@"y = %0.2f * e^(%0.5f*x)*cos(%0.10f*x) + %0.0f over %d frames", startValue - endValue, alpha, omega, endValue, steps);
NSMutableArray *values = [NSMutableArray arrayWithCapacity:steps];
CGFloat value = 0;
CGFloat oscillationComponent;
CGFloat coefficient;
// conforms to y = A * e^(-alpha*t)*cos(omega*t)
for (NSInteger t = 0; t < steps; t++) {
//decaying mass-spring-damper solution with initial displacement
if (self.shake) {
oscillationComponent = sin(omega*t);
} else {
oscillationComponent = cos(omega*t);
}
coefficient = (startValue - endValue);
if (!self.shouldOvershoot) {
oscillationComponent = fabs(oscillationComponent);
}
value = coefficient * pow(2.71, alpha*t) * oscillationComponent + endValue;
[values addObject:[NSNumber numberWithFloat:value]];
}
return values;
}
@end
#pragma clang diagnostic pop