forked from pranavlathigara/UIView-TNRAnimationHelper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUIView+TNRAnimationHelper.m
121 lines (90 loc) · 3.99 KB
/
UIView+TNRAnimationHelper.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
//
// Created by Alexandr Panchenko on 14/10/16.
// Copyright (c) 2016 Thunderrise. All rights reserved.
//
// Licensed under the MIT license: http://opensource.org/licenses/MIT
// Latest version can be found at https://github.com/thunderrise/UIView-TNRAnimationHelper
//
#import "UIView+TNRAnimationHelper.h"
@implementation UIView (TNRAnimationHelper)
- (void)tnr_shakeHorizontally {
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.translation.x"];
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
animation.duration = 0.5f;
animation.values = @[@(-12), @(12), @(-8), @(8), @(-4), @(4), @(0)];
[self.layer addAnimation:animation forKey:@"shake"];
}
- (void)tnr_shakeVertically {
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.translation.y"];
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
animation.duration = 0.5f;
animation.values = @[@(-12), @(12), @(-8), @(8), @(-4), @(4), @(0)];
[self.layer addAnimation:animation forKey:@"shake"];
}
- (void)tnr_pulseToSize:(CGFloat)scale
duration:(NSTimeInterval)duration
repeat:(BOOL)repeat {
CABasicAnimation *pulseAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
pulseAnimation.duration = duration;
pulseAnimation.toValue = [NSNumber numberWithFloat:scale];
pulseAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
pulseAnimation.autoreverses = YES;
pulseAnimation.repeatCount = repeat ? HUGE_VALF : 0;
[self.layer addAnimation:pulseAnimation
forKey:@"pulse"];
}
- (void)tnr_flipWithDuration:(NSTimeInterval)duration
direction:(UIViewAnimationFlipDirection)direction
repeatCount:(NSUInteger)repeatCount
autoreverse:(BOOL)shouldAutoreverse {
NSString *subtype = nil;
switch (direction) {
case UIViewAnimationFlipDirectionFromTop:
subtype = @"fromTop";
break;
case UIViewAnimationFlipDirectionFromLeft:
subtype = @"fromLeft";
break;
case UIViewAnimationFlipDirectionFromBottom:
subtype = @"fromBottom";
break;
case UIViewAnimationFlipDirectionFromRight:
default:
subtype = @"fromRight";
break;
}
CATransition *transition = [CATransition animation];
transition.startProgress = 0.0f;
transition.endProgress = 1.0f;
transition.type = @"flip";
transition.subtype = subtype;
transition.duration = duration;
transition.repeatCount = repeatCount;
transition.autoreverses = shouldAutoreverse;
[self.layer addAnimation:transition
forKey:@"spin"];
}
- (void)tnr_rotateToAngle:(CGFloat)angle
duration:(NSTimeInterval)duration
direction:(UIViewAnimationRotationDirection)direction
repeatCount:(NSUInteger)repeatCount
autoreverse:(BOOL)shouldAutoreverse {
CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.toValue = @(direction == UIViewAnimationRotationDirectionRight ? angle * M_PI / 180.0 : - (angle * M_PI / 180.0));
rotationAnimation.duration = duration;
rotationAnimation.autoreverses = shouldAutoreverse;
rotationAnimation.repeatCount = repeatCount;
rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[self.layer addAnimation:rotationAnimation
forKey:@"transform.rotation.z"];
}
- (void)tnr_stopAnimation {
[CATransaction begin];
[self.layer removeAllAnimations];
[CATransaction commit];
[CATransaction flush];
}
- (BOOL)tnr_isBeingAnimated {
return [self.layer.animationKeys count];
}
@end