-
Notifications
You must be signed in to change notification settings - Fork 32
/
ZMaterialButton.m
99 lines (82 loc) · 3.27 KB
/
ZMaterialButton.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
//
// ZMaterialButton.m
//
//
// Created by Ricardo Zertuche on 7/25/15.
//
//
#import "ZMaterialButton.h"
@implementation ZMaterialButton
- (ZMaterialButton*)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
self.layer.cornerRadius = frame.size.width/2;
self.imageView.contentMode = UIViewContentModeCenter;
expanded = false;
originalFrame = frame;
originalImage = self.imageView.image;
self.expandBy = 9.0;
self.delegate = self;
return self;
}
- (void)setEndAnimationPoint:(CGPoint)ePoint{
endPoint = ePoint;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
if (!expanded) {
[self buttonMove];
} else {
[self buttonReduce];
}
}
- (void)buttonMove{
[UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
self.frame = CGRectMake(endPoint.x, endPoint.y, self.frame.size.width, self.frame.size.height);
} completion:^(BOOL finished){
[self buttonExpand];
}];
}
- (void)buttonExpand{
self.imageView.alpha = 0;
UIImageView *dummyImageView = [[UIImageView alloc]initWithFrame:self.superview.frame];
[dummyImageView setImage:self.changeToImage];
[dummyImageView setContentMode:UIViewContentModeCenter];
[self.superview addSubview:dummyImageView];
[UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
self.transform = CGAffineTransformMakeScale(self.expandBy,self.expandBy);
} completion:^(BOOL finished){
expanded = true;
self.transform = CGAffineTransformMakeScale(1.0,1.0);
self.imageView.image = self.changeToImage;
self.imageView.alpha = 1;
self.superview.backgroundColor = self.backgroundColor;
[dummyImageView removeFromSuperview];
[self.delegate ZMaterialButtonDidExpandButton:self withSuccces:true];
}];
}
- (void)buttonReduce{
self.imageView.alpha = 0;
self.transform = CGAffineTransformMakeScale(self.expandBy,self.expandBy);
self.superview.backgroundColor = self.originalParentViewColor;
UIView *dummyView = self.superview;
UIImageView *dummyImageView = [[UIImageView alloc]initWithFrame:dummyView.frame];
[dummyImageView setImage:self.changeToImage];
[dummyImageView setContentMode:UIViewContentModeCenter];
[dummyView addSubview:dummyImageView];
[UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
self.transform = CGAffineTransformMakeScale(1,1);
} completion:^(BOOL finished){
expanded = false;
self.imageView.image = originalImage;
self.imageView.alpha = 1;
[dummyImageView removeFromSuperview];
[self buttonGetBack];
}];
}
- (void)buttonGetBack{
[UIView animateWithDuration:0.8 delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
self.frame = originalFrame;
} completion:^(BOOL finished){
[self.delegate ZMaterialButtonDidExpandButton:self withSuccces:false];
}];
}
@end