-
Notifications
You must be signed in to change notification settings - Fork 186
/
Copy pathPopMenuButton.m
236 lines (210 loc) · 8.51 KB
/
PopMenuButton.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
//
// PopMenuButton.m
// HyPopMenuView
//
// Created by Hy_Mac on 16/7/8.
// Copyright © 2016年 ouy.Aberi. All rights reserved.
//
#import "PopMenuButton.h"
#import "UIColor+ImageGetColor.h"
@implementation PopMenuButton
static NSString* animationKey = @"transform.scale";
+ (instancetype __nonnull)buttonWithType:(UIButtonType)buttonType
{
PopMenuButton* button = [super buttonWithType:buttonType];
return button;
}
- (instancetype __nonnull)init
{
self = [super initWithFrame:CGRectNull];
if (self) {
self.titleLabel.alpha = 0.0f;
//1.文字居中
self.titleLabel.textAlignment = NSTextAlignmentCenter;
//2.文字大小
self.titleLabel.font = [UIFont systemFontOfSize:14];
//3.图片的内容模式
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
self.imageView.clipsToBounds = true;
//4.取消高亮
self.adjustsImageWhenHighlighted = false;
[self addTarget:self action:@selector(scaleToSmall)
forControlEvents:UIControlEventTouchDown | UIControlEventTouchDragEnter];
[self addTarget:self action:@selector(scaleToDefault)
forControlEvents:UIControlEventTouchDragExit];
self.layer.masksToBounds = true;
}
return self;
}
- (void)setTextLabelWithShadowColor:(UIColor*)color
{
UILabel* textLabel = [self viewWithTag:21];
if (!textLabel) {
textLabel = [UILabel new];
textLabel.tag = 21;
textLabel.textAlignment = NSTextAlignmentCenter;
textLabel.font = [UIFont systemFontOfSize:14];
[self addSubview:textLabel];
}
[textLabel setFrame:CGRectMake(0, CGRectGetHeight(self.bounds) - 20, CGRectGetWidth(self.bounds), 20)];
[textLabel setTextColor:color];
[textLabel setText:_model.titleString];
textLabel.shadowColor = [UIColor whiteColor];
textLabel.shadowOffset = CGSizeMake(0.3f, 0.3f);
}
- (void)scaleToSmall
{
CABasicAnimation* theAnimation;
theAnimation = [CABasicAnimation animationWithKeyPath:animationKey];
theAnimation.delegate = self;
theAnimation.duration = 0.1;
theAnimation.repeatCount = 0;
theAnimation.removedOnCompletion = FALSE;
theAnimation.fillMode = kCAFillModeForwards;
theAnimation.autoreverses = NO;
theAnimation.fromValue = [NSNumber numberWithFloat:1];
theAnimation.toValue = [NSNumber numberWithFloat:1.2f];
[self.imageView.layer addAnimation:theAnimation forKey:theAnimation.keyPath];
}
- (void)scaleToDefault
{
CABasicAnimation* theAnimation;
theAnimation = [CABasicAnimation animationWithKeyPath:animationKey];
theAnimation.delegate = self;
theAnimation.duration = 0.1;
theAnimation.repeatCount = 0;
theAnimation.removedOnCompletion = FALSE;
theAnimation.fillMode = kCAFillModeForwards;
theAnimation.autoreverses = NO;
theAnimation.fromValue = [NSNumber numberWithFloat:1.2f];
theAnimation.toValue = [NSNumber numberWithFloat:1];
[self.imageView.layer addAnimation:theAnimation forKey:theAnimation.keyPath];
}
- (void)selectdAnimation
{
if (_model.transitionType == PopMenuTransitionTypeSystemApi) {
CABasicAnimation* scaleAnimation = [CABasicAnimation animationWithKeyPath:animationKey];
scaleAnimation.delegate = self;
scaleAnimation.duration = 0.2;
scaleAnimation.repeatCount = 0;
scaleAnimation.removedOnCompletion = FALSE;
scaleAnimation.fillMode = kCAFillModeForwards;
scaleAnimation.autoreverses = NO;
scaleAnimation.fromValue = @1;
scaleAnimation.toValue = @1.4;
CABasicAnimation* opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
opacityAnimation.delegate = self;
opacityAnimation.duration = 0.2;
opacityAnimation.repeatCount = 0;
opacityAnimation.removedOnCompletion = FALSE;
opacityAnimation.fillMode = kCAFillModeForwards;
opacityAnimation.autoreverses = NO;
opacityAnimation.fromValue = @1;
opacityAnimation.toValue = @0;
[self.layer addAnimation:scaleAnimation forKey:scaleAnimation.keyPath];
[self.layer addAnimation:opacityAnimation forKey:opacityAnimation.keyPath];
}
else {
self.userInteractionEnabled = false;
self.layer.cornerRadius = CGRectGetWidth(self.bounds) / 2;
NSLog(@"%.2f",CGRectGetWidth(self.bounds) / 2);
UIImage* image = self.imageView.image;
UIColor* color = [UIColor getPixelColorAtLocation:CGPointMake(50, 20) inImage:image];
[self setBackgroundColor:color];
if (_model.transitionRenderingColor) {
[self setBackgroundColor:_model.transitionRenderingColor];
}
UILabel* textLabel = [self viewWithTag:21];
textLabel.text = @"";
[self setTitle:@"" forState:UIControlStateNormal];
[self setImage:nil forState:UIControlStateNormal];
CABasicAnimation* expandAnim = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
expandAnim.fromValue = @(1.0);
expandAnim.toValue = @(33.0);
expandAnim.timingFunction = [CAMediaTimingFunction functionWithControlPoints:0.95:0.02:1:0.05];
expandAnim.duration = 0.3;
expandAnim.delegate = self;
expandAnim.fillMode = kCAFillModeForwards;
expandAnim.removedOnCompletion = false;
expandAnim.autoreverses = NO;
[self.layer addAnimation:expandAnim forKey:expandAnim.keyPath];
}
}
- (void)cancelAnimation
{
CABasicAnimation* scaleAnimation = [CABasicAnimation animationWithKeyPath:animationKey];
scaleAnimation.delegate = self;
scaleAnimation.duration = 0.3;
scaleAnimation.repeatCount = 0;
scaleAnimation.removedOnCompletion = FALSE;
scaleAnimation.fillMode = kCAFillModeForwards;
scaleAnimation.autoreverses = NO;
scaleAnimation.fromValue = @1;
scaleAnimation.toValue = @0.3;
CABasicAnimation* opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
opacityAnimation.delegate = self;
opacityAnimation.duration = 0.3;
opacityAnimation.beginTime = 0;
opacityAnimation.repeatCount = 0;
opacityAnimation.removedOnCompletion = false;
opacityAnimation.fillMode = kCAFillModeForwards;
opacityAnimation.autoreverses = NO;
opacityAnimation.fromValue = @1;
opacityAnimation.toValue = @0;
//CGAffineTransformIdentity
[self.layer addAnimation:scaleAnimation forKey:[NSString stringWithFormat:@"cancel%@", scaleAnimation.keyPath]];
[self.layer addAnimation:opacityAnimation forKey:opacityAnimation.keyPath];
}
- (void)animationDidStop:(CAAnimation*)anim finished:(BOOL)flag
{
CABasicAnimation* cab = (CABasicAnimation*)anim;
if ([cab.toValue floatValue] == 33.0f || [cab.toValue floatValue] == 1.4f) {
[self setUserInteractionEnabled:true];
__weak PopMenuButton* weakButton = self;
if (weakButton.block) {
weakButton.block(self);
}
[NSTimer scheduledTimerWithTimeInterval:0.6f target:self selector:@selector(DidStopAnimation) userInfo:nil repeats:nil];
}
}
- (void)DidStopAnimation
{
[self.layer removeAllAnimations];
}
#pragma mark 调整内部ImageView的frame
- (CGRect)imageRectForContentRect:(CGRect)contentRect
{
CGFloat imageWidth = contentRect.size.width / 1.7;
CGFloat imageX = CGRectGetWidth(contentRect) / 2 - imageWidth / 2;
CGFloat imageHeight = imageWidth;
CGFloat imageY = CGRectGetHeight(self.bounds) - (imageHeight + 30);
return CGRectMake(imageX, imageY, imageWidth, imageHeight);
}
#pragma mark 调整内部UILabel的frame
- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
CGFloat titleX = 0;
CGFloat titleHeight = 20;
CGFloat titleY = contentRect.size.height - titleHeight;
CGFloat titleWidth = contentRect.size.width;
return CGRectMake(titleX, titleY, titleWidth, titleHeight);
}
- (void)setModel:(PopMenuModel*)model
{
_model = model;
UIImage* image = [UIImage imageNamed:model.imageNameString];
[self setImage:image forState:UIControlStateNormal];
[self setTitle:model.titleString forState:UIControlStateNormal];
UIColor* tempColor = nil;
UIColor* color = [UIColor getPixelColorAtLocation:CGPointMake(image.size.width / 2, image.size.height / 2) inImage:image];
// [self setTitleColor:color forState:UIControlStateNormal];
tempColor = color;
if (!_model.automaticIdentificationColor) {
if (_model.textColor) {
//[self setTitleColor:model.textColor forState:UIControlStateNormal];
tempColor = model.textColor;
}
}
[self setTextLabelWithShadowColor:tempColor];
}
@end