-
Notifications
You must be signed in to change notification settings - Fork 13
/
UIView+Shake.m
207 lines (163 loc) · 6.61 KB
/
UIView+Shake.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
//
// UIView+Shake.m
// Mitch
//
// Created by Simon CORSIN on 11/07/14.
// Copyright (c) 2014 Mindie. All rights reserved.
//
#import <objc/runtime.h>
#import "UIView+Shake.h"
#define HAS_OPT(options, option) ((options & option) == option)
@interface SCShakeInfo : NSObject
@property (assign, nonatomic) CGAffineTransform baseTransform;
@property (assign, nonatomic) BOOL shaking;
@property (assign, nonatomic) SCShakeOptions options;
@property (assign, nonatomic) CGFloat force;
@property (assign, nonatomic) CGFloat duration;
@property (assign, nonatomic) CGFloat iterationDuration;
@property (assign, nonatomic) CFTimeInterval startTime;
@property (readonly, nonatomic) CGFloat completionRatio;
@property (strong, nonatomic) ShakeCompletionHandler completionHandler;
@property (assign, nonatomic) BOOL reversed;
@end
@implementation SCShakeInfo
- (CGFloat)completionRatio {
return (CACurrentMediaTime() - self.startTime) / self.duration;
}
@end
@implementation UIView (Shake)
static const char *ShakeInfoKey = "ShakeInfo";
- (void)shake {
[self shakeWithOptions:kDefaultShakeOptions force:kDefaultShakeForce duration:kDefaultShakeDuration iterationDuration:kDefaultShakeIterationDuration completionHandler:nil];
}
- (void)shakeWithOptions:(SCShakeOptions)shakeOptions force:(CGFloat)force duration:(CGFloat)duration iterationDuration:(CGFloat)iterationDuration completionHandler:(ShakeCompletionHandler)completionHandler {
SCShakeInfo *shakeInfo = [self shakeInfo];
shakeInfo.options = shakeOptions;
shakeInfo.force = force;
shakeInfo.startTime = CACurrentMediaTime();
shakeInfo.duration = duration;
shakeInfo.iterationDuration = iterationDuration;
shakeInfo.completionHandler = completionHandler;
if (!shakeInfo.shaking) {
shakeInfo.baseTransform = self.transform;
shakeInfo.shaking = YES;
[self _doAnimation:1];
}
}
- (CGFloat)_getInterpolationRatio:(CGFloat)completionRatio options:(SCShakeOptions)options {
CGFloat (*interpFunc)(CGFloat) = nil;
if (HAS_OPT(options, SCShakeOptionsForceInterpolationRandom)) {
interpFunc = &InterpolateRandom;
} else if (HAS_OPT(options, SCShakeOptionsForceInterpolationExpDown)) {
interpFunc = &InterpolateExpDown;
} else if (HAS_OPT(options, SCShakeOptionsForceInterpolationExpUp)) {
interpFunc = &InterpolateExpUp;
} else if (HAS_OPT(options, SCShakeOptionsForceInterpolationLinearDown)) {
interpFunc = &InterpolateLinearDown;
} else if (HAS_OPT(options, SCShakeOptionsForceInterpolationLinearUp)) {
interpFunc = &InterpolateLinearUp;
} else {
interpFunc = &InterpolateNone;
}
return interpFunc(completionRatio);
}
- (void)_animate:(CGFloat)force shakeInfo:(SCShakeInfo *)shakeInfo {
CGAffineTransform baseTransform = shakeInfo.baseTransform;
SCShakeOptions options = shakeInfo.options;
if (HAS_OPT(options, SCShakeOptionsDirectionHorizontalAndVertical)) {
if (arc4random_uniform(2) == 1) {
self.transform = CGAffineTransformTranslate(baseTransform, 0, force * self.bounds.size.height);
} else {
self.transform = CGAffineTransformTranslate(baseTransform, force * self.bounds.size.width, 0);
}
} else if (HAS_OPT(options, SCShakeOptionsDirectionVertical)) {
self.transform = CGAffineTransformTranslate(baseTransform, 0, force * self.bounds.size.height);
} else if (HAS_OPT(options, SCShakeOptionsDirectionHorizontal)) {
self.transform = CGAffineTransformTranslate(baseTransform, force * self.bounds.size.width, 0);
} else {
self.transform = CGAffineTransformRotate(baseTransform, force * M_PI_2);
}
}
- (void)_doAnimation:(CGFloat)direction {
SCShakeInfo *shakeInfo = [self shakeInfo];
SCShakeOptions options = shakeInfo.options;
CGFloat completionRatio = shakeInfo.completionRatio;
if (completionRatio > 1) {
completionRatio = 1;
}
if (shakeInfo.reversed) {
completionRatio = 1 - completionRatio;
}
CGFloat interpolationRatio = [self _getInterpolationRatio:completionRatio options:options];
CGFloat force = shakeInfo.force * interpolationRatio * direction;
CGFloat iterationDuration = shakeInfo.iterationDuration;
[UIView animateWithDuration:iterationDuration animations:^{
[self _animate:force shakeInfo:shakeInfo];
} completion:^(BOOL finished) {
if (shakeInfo.shaking) {
BOOL shouldRecurse = YES;
if (shakeInfo.completionRatio > 1) {
if (HAS_OPT(shakeInfo.options, SCShakeOptionsAutoreverse)) {
shakeInfo.reversed = !shakeInfo.reversed;
}
if (shakeInfo.reversed || HAS_OPT(shakeInfo.options, SCShakeOptionsAtEndRestart)) {
shakeInfo.startTime = CACurrentMediaTime();
} else if (!HAS_OPT(shakeInfo.options, SCShakeOptionsAtEndContinue)) {
shouldRecurse = NO;
[self endShake];
}
}
if (shouldRecurse) {
[self _doAnimation:direction * -1];
}
}
}];
}
- (void)endShake {
SCShakeInfo *shakeInfo = [self shakeInfo];
if (shakeInfo.shaking) {
shakeInfo.shaking = NO;
self.transform = shakeInfo.baseTransform;
ShakeCompletionHandler completionHandler = shakeInfo.completionHandler;
shakeInfo.completionHandler = nil;
if (completionHandler != nil) {
completionHandler();
}
}
}
- (BOOL)isShaking {
return [self shakeInfo].shaking;
}
- (SCShakeInfo *)shakeInfo {
SCShakeInfo *shakeInfo = objc_getAssociatedObject(self, ShakeInfoKey);
if (shakeInfo == nil) {
shakeInfo = [SCShakeInfo new];
objc_setAssociatedObject(self, ShakeInfoKey, shakeInfo, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
return shakeInfo;
}
#pragma Interpolations functions
static CGFloat InterpolateLinearUp(CGFloat input) {
return input;
}
static CGFloat InterpolateLinearDown(CGFloat input) {
return 1 - input;
}
static CGFloat Exp(CGFloat a, int power) {
if (a <= 0.5f) return (float)pow(a * 2, power) / 2;
return (float)pow((a - 1) * 2, power) / (power % 2 == 0 ? -2 : 2) + 1;
}
static CGFloat InterpolateExpUp(CGFloat input) {
return Exp(input, 4);
}
static CGFloat InterpolateExpDown(CGFloat input) {
return Exp(1 - input, 4);
}
static CGFloat InterpolateNone(CGFloat input) {
return 1;
}
static CGFloat InterpolateRandom(CGFloat input) {
CGFloat randNb = arc4random_uniform(10000);
return randNb / 10000.0;
}
@end