-
Notifications
You must be signed in to change notification settings - Fork 78
/
HyLoglnButton.m
208 lines (162 loc) · 7.19 KB
/
HyLoglnButton.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
//
// HyLoglnButton.m
// Example
//
// Created by 东海 on 15/9/2.
// Copyright (c) 2015年 Jonathan Tribouharet. All rights reserved.
//
#import "HyLoglnButton.h"
@interface HyLoglnButton ()
@property (nonatomic,assign) CFTimeInterval shrinkDuration;
@property (nonatomic,retain) CAMediaTimingFunction *shrinkCurve;
@property (nonatomic,retain) CAMediaTimingFunction *expandCurve;
@property (nonatomic,strong) Completion block;
@property (nonatomic,retain) UIColor *color;
@end
@implementation HyLoglnButton
-(instancetype) initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
_spiner = [[SpinerLayer alloc] initWithFrame:self.frame];
_shrinkCurve = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
_expandCurve = [CAMediaTimingFunction functionWithControlPoints:0.95 :0.02 :1 :0.05];
self.shrinkDuration = 0.1;
[self.layer addSublayer:_spiner];
[self setup];
}
return self;
}
-(void)setCompletion:(Completion)completion
{
_block = completion;
}
-(void)setup{
self.layer.cornerRadius = CGRectGetHeight(self.bounds) / 2;
self.clipsToBounds = true;
[self addTarget:self action:@selector(scaleToSmall)
forControlEvents:UIControlEventTouchDown | UIControlEventTouchDragEnter];
[self addTarget:self action:@selector(scaleAnimation)
forControlEvents:UIControlEventTouchUpInside];
[self addTarget:self action:@selector(scaleToDefault)
forControlEvents:UIControlEventTouchDragExit];
}
- (void)scaleToSmall
{
typeof(self) __weak weak = self;
self.transform = CGAffineTransformMakeScale(1, 1);
[UIView animateWithDuration:0.3 delay:0 usingSpringWithDamping:0.5f initialSpringVelocity:0.0f options:UIViewAnimationOptionLayoutSubviews animations:^{
weak.transform = CGAffineTransformMakeScale(0.9, 0.9);
} completion:^(BOOL finished) {
}];
}
- (void)scaleAnimation
{
typeof(self) __weak weak = self;
[UIView animateWithDuration:0.3 delay:0 usingSpringWithDamping:0.5f initialSpringVelocity:0.0f options:UIViewAnimationOptionLayoutSubviews animations:^{
weak.transform = CGAffineTransformMakeScale(1, 1);
} completion:^(BOOL finished) {
}];
[self StartAnimation];
}
- (void)scaleToDefault
{
typeof(self) __weak weak = self;
[UIView animateWithDuration:0.3 delay:0 usingSpringWithDamping:0.5f initialSpringVelocity:0.4f options:UIViewAnimationOptionLayoutSubviews animations:^{
weak.transform = CGAffineTransformMakeScale(1, 1);
} completion:^(BOOL finished) {
}];
}
-(void)StartAnimation{
[self performSelector:@selector(Revert) withObject:nil afterDelay:0.f];
[self.layer addSublayer:_spiner];
CABasicAnimation *shrinkAnim = [CABasicAnimation animationWithKeyPath:@"bounds.size.width"];
shrinkAnim.fromValue = @(CGRectGetWidth(self.bounds));
shrinkAnim.toValue = @(CGRectGetHeight(self.bounds));
shrinkAnim.duration = _shrinkDuration;
shrinkAnim.timingFunction = _shrinkCurve;
shrinkAnim.fillMode = kCAFillModeForwards;
shrinkAnim.removedOnCompletion = false;
[self.layer addAnimation:shrinkAnim forKey:shrinkAnim.keyPath];
[_spiner animation];
[self setUserInteractionEnabled:false];
}
-(void)ErrorRevertAnimationCompletion:(Completion)completion
{
_block = completion;
CABasicAnimation *shrinkAnim = [CABasicAnimation animationWithKeyPath:@"bounds.size.width"];
shrinkAnim.fromValue = @(CGRectGetHeight(self.bounds));
shrinkAnim.toValue = @(CGRectGetWidth(self.bounds));
shrinkAnim.duration = _shrinkDuration;
shrinkAnim.timingFunction = _shrinkCurve;
shrinkAnim.fillMode = kCAFillModeForwards;
shrinkAnim.removedOnCompletion = false;
_color = self.backgroundColor;
CABasicAnimation *backgroundColor = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];
backgroundColor.toValue = (__bridge id)[UIColor redColor].CGColor;
backgroundColor.duration = 0.1f;
backgroundColor.timingFunction = _shrinkCurve;
backgroundColor.fillMode = kCAFillModeForwards;
backgroundColor.removedOnCompletion = false;
CAKeyframeAnimation *keyFrame = [CAKeyframeAnimation animationWithKeyPath:@"position"];
CGPoint point = self.layer.position;
keyFrame.values = @[[NSValue valueWithCGPoint:CGPointMake(point.x, point.y)],
[NSValue valueWithCGPoint:CGPointMake(point.x - 10, point.y)],
[NSValue valueWithCGPoint:CGPointMake(point.x + 10, point.y)],
[NSValue valueWithCGPoint:CGPointMake(point.x - 10, point.y)],
[NSValue valueWithCGPoint:CGPointMake(point.x + 10, point.y)],
[NSValue valueWithCGPoint:CGPointMake(point.x - 10, point.y)],
[NSValue valueWithCGPoint:CGPointMake(point.x + 10, point.y)],
[NSValue valueWithCGPoint:point]];
keyFrame.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
keyFrame.duration = 0.5f;
keyFrame.delegate = self;
self.layer.position = point;
[self.layer addAnimation:backgroundColor forKey:backgroundColor.keyPath];
[self.layer addAnimation:keyFrame forKey:keyFrame.keyPath];
[self.layer addAnimation:shrinkAnim forKey:shrinkAnim.keyPath];
[_spiner stopAnimation];
[self setUserInteractionEnabled:true];
}
-(void)ExitAnimationCompletion:(Completion)completion{
_block = completion;
CABasicAnimation *expandAnim = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
expandAnim.fromValue = @(1.0);
expandAnim.toValue = @(33.0);
expandAnim.timingFunction = _expandCurve;
expandAnim.duration = 0.3;
expandAnim.delegate = self;
expandAnim.fillMode = kCAFillModeForwards;
expandAnim.removedOnCompletion = false;
[self.layer addAnimation:expandAnim forKey:expandAnim.keyPath];
[_spiner stopAnimation];
}
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
CABasicAnimation *cab = (CABasicAnimation *)anim;
if ([cab.keyPath isEqualToString:@"transform.scale"]) {
[self setUserInteractionEnabled:true];
if (_block) {
_block();
}
[NSTimer scheduledTimerWithTimeInterval:0.5f target:self selector:@selector(DidStopAnimation) userInfo:nil repeats:nil];
}
}
-(void)Revert{
CABasicAnimation *backgroundColor = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];
backgroundColor.toValue = (__bridge id)self.backgroundColor.CGColor;
backgroundColor.duration = 0.1f;
backgroundColor.timingFunction = _shrinkCurve;
backgroundColor.fillMode = kCAFillModeForwards;
backgroundColor.removedOnCompletion = false;
[self.layer addAnimation:backgroundColor forKey:@"backgroundColors"];
}
-(void)DidStopAnimation{
[self.layer removeAllAnimations];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
@end