-
Notifications
You must be signed in to change notification settings - Fork 0
/
VLMHarlemShakeModified.m
311 lines (231 loc) · 10.3 KB
/
VLMHarlemShakeModified.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
//
// VLMHarlemShake.m
// HarlemShakeDemo
//
// Created by Zac White on 3/5/13.
// Copyright (c) 2013 Velos Mobile. All rights reserved.
//
//
// Modified by J.K. Hayslip (iKilledAppl3)
#import "VLMHarlemShake.h"
#import <AVFoundation/AVFoundation.h>
#import <objc/runtime.h>
// Private classes Apple uses for tvOS icons
@interface _UIStackedImageContainerView : UIView
@end
@interface TVImageStackView : UIView
@end
@interface UINamedLayerStack : NSObject
@end
typedef enum {
VLMShakeStyleOne = 0,
VLMShakeStyleTwo,
VLMShakeStyleThree,
VLMShakeStyleEnd
} VLMShakeStyle;
@interface VLMHarlemShake () <AVAudioPlayerDelegate>
@property (nonatomic, strong) UIView *lonerView;
@property (nonatomic, strong) NSMutableArray *shakingViews;
@property (nonatomic, copy) void(^completionBlock)();
@property (nonatomic, strong) AVAudioPlayer *audioPlayer;
@property (nonatomic, assign, getter = isShaking) BOOL shaking;
// finds views that we might want to shake.
- (void)_findViewsOfInterestWithCallback:(void(^)(UIView *viewOfInterest))callback;
- (void)_findViewsOfInterestInView:(UIView *)rootView withCallback:(void(^)(UIView *viewOfInterest))callback;
// actually shakes the view.
- (void)_shakeView:(UIView *)view withShakeStyle:(VLMShakeStyle)style randomSeed:(CGFloat)seed;
// animation creation methods.
- (CAAnimation *)animationForStyleOneWithSeed:(CGFloat)seed;
- (CAAnimation *)animationForStyleTwoWithSeed:(CGFloat)seed;
- (CAAnimation *)animationForStyleThreeWithSeed:(CGFloat)seed;
@end
@implementation VLMHarlemShake
- (id)initWithLonerView:(UIView *)lonerView
{
if (!(self = [self init])) return nil;
self.lonerView = lonerView;
return self;
}
- (id)init
{
if (!(self = [super init])) return nil;
self.shakingViews = [NSMutableArray array];
self.shaking = NO;
//the only thing I modified to get it to work for tvOS
NSURL *audioURL = [NSURL URLWithString:@"/Library/TVSystemMenuModules/HarlemShakeTV.bundle/HarlemShake.mp3"];
NSError *error = nil;
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:&error];
self.audioPlayer.delegate = self;
[self.audioPlayer prepareToPlay];
if (!self.audioPlayer && error) {
NSLog(@"ERROR: %@", error);
return nil;
}
return self;
}
- (void)shakeWithCompletion:(void(^)())completion
{
if (self.shaking) return;
self.shaking = YES;
self.completionBlock = completion;
// start playing the harlem shake track.
[self.audioPlayer play];
// inspect the hierarchy.
self.shakingViews = [NSMutableArray array];
// shake the loner view.
[self _shakeView:self.lonerView withShakeStyle:VLMShakeStyleThree randomSeed:(arc4random() / (CGFloat)RAND_MAX)];
float delayInSeconds = 14.5;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[self _findViewsOfInterestWithCallback:^(UIView *viewOfInterest) {
[self.shakingViews addObject:viewOfInterest];
// shake the view with a random animation.
[self _shakeView:viewOfInterest withShakeStyle:(rand() % VLMShakeStyleEnd) randomSeed:(arc4random() / (CGFloat)RAND_MAX)];
// shake it with another to create more animations.
[self _shakeView:viewOfInterest withShakeStyle:(rand() % VLMShakeStyleEnd) randomSeed:(arc4random() / (CGFloat)RAND_MAX)];
}];
});
}
- (void)_findViewsOfInterestWithCallback:(void(^)(UIView *viewOfInterest))callback
{
// find the top view.
UIView *topView = self.lonerView;
while ([topView superview]) {
topView = [topView superview];
}
[self _findViewsOfInterestInView:topView withCallback:callback];
}
- (void)_findViewsOfInterestInView:(UIView *)rootView withCallback:(void(^)(UIView *viewOfInterest))callback
{
for (UIView *view in [rootView subviews]) {
if ([view respondsToSelector:@selector(_iconImageView)] ||
[view isKindOfClass:[UILabel class]] ||
[view isKindOfClass:[UIButton class]] ||
[view isKindOfClass:[UIImageView class]] ||
[view isKindOfClass:[UISwitch class]] ||
[view isKindOfClass:[UITableViewCell class]] ||
[view isKindOfClass:[UIProgressView class]] ||
[view isKindOfClass:[UITextField class]] ||
[view isKindOfClass:[UISlider class]] ||
[view isKindOfClass:[UITextView class]] ||
[view isKindOfClass:[UICollectionView class]] ||
[view isKindOfClass:[UICollectionViewCell class]] ||
[view isKindOfClass:[_UIStackedImageContainerView class]] ||
[view isKindOfClass:[NSClassFromString(@"TVImageStackView") class]] ||
[view isKindOfClass:[NSClassFromString(@"UINamedLayerStack") class]]) {
if (callback) callback(view);
} else {
[self _findViewsOfInterestInView:view withCallback:callback];
}
}
}
- (void)_shakeView:(UIView *)view withShakeStyle:(VLMShakeStyle)style randomSeed:(CGFloat)seed
{
if (style == VLMShakeStyleOne) {
[view.layer addAnimation:[self animationForStyleOneWithSeed:seed] forKey:@"styleOne"];
} else if (style == VLMShakeStyleTwo) {
[view.layer addAnimation:[self animationForStyleTwoWithSeed:seed] forKey:@"styleTwo"];
} else if (style == VLMShakeStyleThree) {
[view.layer addAnimation:[self animationForStyleThreeWithSeed:seed] forKey:@"styleThree"];
}
self.shaking = NO;
}
- (CAAnimation *)animationForStyleOneWithSeed:(CGFloat)seed
{
CAAnimationGroup *styleOneGroup = [CAAnimationGroup animation];
CABasicAnimation *rotate = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
if (seed < 0.5) {
rotate.fromValue = @(M_PI * 2);
rotate.toValue = @(0);
} else {
rotate.fromValue = @(0);
rotate.toValue = @(M_PI * 2);
}
rotate.duration = 1.0 + seed;
CABasicAnimation *pop = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
pop.fromValue = @(1);
pop.toValue = @(1.2);
pop.beginTime = rotate.duration;
pop.duration = 0.5 + seed;
pop.autoreverses = YES;
pop.repeatCount = 1;
styleOneGroup.repeatCount = 10;
styleOneGroup.autoreverses = YES;
styleOneGroup.duration = rotate.duration + pop.duration;
styleOneGroup.animations = @[rotate, pop];
return styleOneGroup;
}
- (CAAnimation *)animationForStyleTwoWithSeed:(CGFloat)seed
{
CAKeyframeAnimation *keyFrameShake = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
CGFloat negative = -1;
if (seed < 0.5) {
negative = 1;
}
CATransform3D startingScale = CATransform3DIdentity;
CATransform3D secondScale = CATransform3DScale(CATransform3DIdentity, 1.0f + (seed * negative), 1.0f + (seed * negative), 1.0f);
CATransform3D thirdScale = CATransform3DScale(CATransform3DIdentity, 1.0f + (seed * -negative), 1.0f + (seed * -negative), 1.0f);
CATransform3D finalScale = CATransform3DIdentity;
keyFrameShake.values = @[[NSValue valueWithCATransform3D:startingScale],
[NSValue valueWithCATransform3D:secondScale],
[NSValue valueWithCATransform3D:thirdScale],
[NSValue valueWithCATransform3D:finalScale]
];
keyFrameShake.keyTimes = @[@(0), @(0.4), @(0.7), @(1.0)];
NSArray *timingFunctions = @[[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut],
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
];
keyFrameShake.timingFunctions = timingFunctions;
keyFrameShake.duration = 1.0 + seed;
keyFrameShake.repeatCount = 100;
return keyFrameShake;
}
- (CAAnimation *)animationForStyleThreeWithSeed:(CGFloat)seed
{
CAKeyframeAnimation *keyFrameShake = [CAKeyframeAnimation animationWithKeyPath:@"transform.translation"];
CGFloat negative = -1;
if (seed < 0.5) {
negative = 1;
}
NSInteger offsetOne = (NSInteger)((10.0 + 20.0 * seed) * negative);
NSInteger offsetTwo = -offsetOne;
NSValue *startingOffset = [NSValue valueWithCGSize:CGSizeZero];
NSValue *firstOffset = [NSValue valueWithCGSize:CGSizeMake(offsetOne, 0)];
NSValue *secondOffset = [NSValue valueWithCGSize:CGSizeMake(offsetTwo, 0)];
NSValue *thirdOffset = [NSValue valueWithCGSize:CGSizeZero];
NSValue *fourthOffset = [NSValue valueWithCGSize:CGSizeMake(0, offsetOne)];
NSValue *fifthOffset = [NSValue valueWithCGSize:CGSizeMake(0, offsetTwo)];
NSValue *finalOffset = [NSValue valueWithCGSize:CGSizeZero];
keyFrameShake.values = @[startingOffset, firstOffset, secondOffset, thirdOffset, fourthOffset, fifthOffset, finalOffset];
keyFrameShake.keyTimes = @[@(0), @(0.1), @(0.3), @(0.4), @(0.5), @(0.7), @(0.8), @(1.0)];
NSArray *timingFunctions = @[[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
];
keyFrameShake.timingFunctions = timingFunctions;
keyFrameShake.duration = 1.0 + seed;
keyFrameShake.repeatCount = 10;
keyFrameShake.removedOnCompletion = YES;
return keyFrameShake;
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
if (flag) {
// reset all the views.
for (UIView *view in self.shakingViews) {
[view.layer removeAllAnimations];
}
if (self.completionBlock) {
self.completionBlock();
}
}
}
-(void)dealloc
{
[_audioPlayer release];
_audioPlayer = nil;
[super dealloc];
}
@end