forked from Boris-Em/BEMCheckBox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBEMAnimationManager.m
109 lines (87 loc) · 3.78 KB
/
BEMAnimationManager.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
//
// BEMAnimationManager.m
// CheckBox
//
// Created by Bobo on 9/19/15.
// Copyright (c) 2015 Boris Emorine. All rights reserved.
//
#import "BEMAnimationManager.h"
@implementation BEMAnimationManager
- (instancetype)initWithAnimationDuration:(CGFloat)animationDuration {
self = [super init];
if (self) {
_animationDuration = animationDuration;
}
return self;
}
- (CABasicAnimation *)strokeAnimationReverse:(BOOL)reverse {
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
if (reverse) {
animation.fromValue = [NSNumber numberWithFloat:1.0];
animation.toValue = [NSNumber numberWithFloat:0.0];
} else {
animation.fromValue = [NSNumber numberWithFloat:0.0];
animation.toValue = [NSNumber numberWithFloat:1.0];
}
animation.duration = self.animationDuration;
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
return animation;
}
- (CABasicAnimation *)opacityAnimationReverse:(BOOL)reverse {
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];
if (reverse) {
animation.fromValue = [NSNumber numberWithFloat:1.0];
animation.toValue = [NSNumber numberWithFloat:0.0];
} else {
animation.fromValue = [NSNumber numberWithFloat:0.0];
animation.toValue = [NSNumber numberWithFloat:1.0];
}
animation.duration = self.animationDuration;
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
return animation;
}
- (CABasicAnimation *)morphAnimationFromPath:(UIBezierPath *)fromPath toPath:(UIBezierPath *)toPath {
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"path"];
animation.duration = self.animationDuration;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation.fromValue = (id)fromPath.CGPath;
animation.toValue = (id)toPath.CGPath;
return animation;
}
- (CAKeyframeAnimation *)fillAnimationWithBounces:(NSUInteger)bounces amplitude:(CGFloat)amplitude reverse:(BOOL)reverse {
NSMutableArray *values = [NSMutableArray new];
NSMutableArray *keyTimes = [NSMutableArray new];
if (reverse) {
[values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1, 1, 1)]];
} else {
[values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0, 0, 0)]];
}
[keyTimes addObject:@0.0];
for (NSUInteger i = 1; i <= bounces; i++) {
CGFloat scale = (i % 2) ? (1 + amplitude/i) : (1 - amplitude/i);
CGFloat time = i * 1.0/(bounces + 1);
[values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(scale, scale, scale)]];
[keyTimes addObject:[NSNumber numberWithFloat:time]];
}
if (reverse) {
[values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.0001, 0.0001, 0.0001)]];
} else {
[values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1, 1, 1)]];
}
[keyTimes addObject:@1.0];
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
animation.values = values;
animation.keyTimes = keyTimes;
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
animation.duration = self.animationDuration;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
return animation;
}
- (void)dealloc {
}
@end