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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
//
// GMButton.m
// Gengmei
//
// Created by Thierry on 12/26/14.
// Copyright (c) 2014 Wanmeichuangyi. All rights reserved.
//
#import "GMButton.h"
#import "GMFont.h"
#import <UIColor+GMTheme.h>
#import <Constant.h>
#import "UIImage+GM.h"
@interface GMButton ()
@property (nonatomic, strong) UIColor *highlightBackgroundColor;
@property (nonatomic, strong) UIColor *normalBackgroundColor;
@property (nonatomic, strong) UIColor *selectedBackgroundColor;
@property (nonatomic, strong) UIColor *disableBackgroundColor;
@property (nonatomic, assign) GMButtonType customType;
@property (nonatomic, strong) UIColor *normalBorderColor;
@property (nonatomic, strong) UIColor *disableBorderColor;
@property (nonatomic, strong) NSArray *normalGradientColors;
@property (nonatomic, strong) NSArray *highlightGradientColors;
@end
@implementation GMButton
#pragma mark - 初始化
-(id)init{
self = [super init];
if (self) {
[self setup];
}
return self;
}
- (id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
[self setup];
}
return self;
}
- (void)awakeFromNib{
[super awakeFromNib];
[self setup];
}
- (void)setup{
_adaptiveHotAreaWidth = 44.0;
_adaptiveHotAreaHeight = 44.0;
_normalBackgroundColor = [UIColor clearColor];
}
#pragma mark - 实例化方法
+ (instancetype)buttonWithTitle:(NSString *)title
backgroundColor:(UIColor *)bgColor
titleFontSize:(CGFloat)size
titleColor:(UIColor *)titleColor{
GMButton *button = [GMButton buttonWithType:UIButtonTypeCustom];
if (bgColor) {
button.backgroundColor = bgColor;
}
if (title) {
[button setTitle:title forState:UIControlStateNormal];
}
button.titleLabel.font = [UIFont gmFont:size ?: 14];
if (titleColor) {
[button setTitleColor:titleColor forState:UIControlStateNormal];
}
return button;
}
+ (instancetype)buttonWithCustomType:(GMButtonType)customType{
GMButton *button = [GMButton buttonWithType:UIButtonTypeCustom];
button.clipsToBounds = YES;
button.customType = customType;
[button setTitle:@"按钮" forState:UIControlStateNormal];
switch (customType) {
case GMButtonTypeGreen:
[button setGreenAppearance];
break;
case GMButtonTypeGreenRoundCorner:
button.layer.cornerRadius = 6 * ONE_PIXEL;
[button setGreenAppearance];
break;
case GMButtonTypeGreenSemicircle:
[button setGreenAppearance];
break;
case GMButtonTypeGreenRoundCornerBorder:
button.layer.borderWidth = ONE_PIXEL;
button.layer.cornerRadius = 6 * ONE_PIXEL;
button.normalBorderColor = UIColor.mainVisual;
button.disableBorderColor = UIColor.disable;
[button setTitleColor:UIColor.mainVisual forState:UIControlStateNormal];
[button setBackgroundColor: UIColor.clearColor forState:UIControlStateNormal | UIControlStateDisabled];
button.enabled = YES;
break;
case GMButtonTypeGreenRoundCornerGradient:
button.layer.cornerRadius = 6 * ONE_PIXEL;
break;
case GMButtonTypeGreenSemicircleGradient:
break;
case GMButtonTypeRed:
[button setRedAppearance];
break;
case GMButtonTypeRedRoundCorner:
[button setRedAppearance];
button.layer.cornerRadius = 6 * ONE_PIXEL;
break;
case GMButtonTypeRedSemicircle:
[button setRedAppearance];
break;
case GMButtonTypeRedRoundCornerBorder:
button.layer.borderWidth = ONE_PIXEL;
button.layer.cornerRadius = 6 * ONE_PIXEL;
button.normalBorderColor = UIColor.secondaryVisual;
button.disableBorderColor = UIColor.disable;
[button setBackgroundColor: UIColor.whiteColor forState:UIControlStateNormal | UIControlStateDisabled];
[button setTitleColor:UIColor.secondaryVisual forState:UIControlStateNormal];
button.enabled = YES;
break;
case GMButtonTypeRedRoundCornerGradient:
button.layer.cornerRadius = 6 * ONE_PIXEL;
break;
case GMButtonTypeRedSemicircleGradient:
break;
case GMButtonTypeGraySemicircleBorder:
button.layer.borderColor = UIColor.separatorLine.CGColor;
button.layer.borderWidth = ONE_PIXEL;
[button setBackgroundColor: UIColor.background forState:UIControlStateNormal];
[button setBackgroundColor: UIColor.separatorLine forState:UIControlStateHighlighted];
[button setTitleColor:UIColor.bodyText forState:UIControlStateNormal];
break;
default:
break;
}
return button;
}
//为半圆的设置半径
- (void)layoutSubviews{
[super layoutSubviews];
if (_customType == GMButtonTypeGreenSemicircle ||
_customType == GMButtonTypeGreenSemicircleBorder ||
_customType == GMButtonTypeGreenSemicircleGradient ||
_customType == GMButtonTypeRedSemicircle ||
_customType == GMButtonTypeRedSemicircleGradient ||
_customType == GMButtonTypeGraySemicircleBorder) {
self.layer.cornerRadius = MIN(self.frame.size.height, self.frame.size.width)/2;
}
}
#pragma mark - Button Appearance
- (void)setGreenAppearance{
[self setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
[self setBackgroundColor:UIColor.buttonNormalGreen forState:UIControlStateNormal];
[self setBackgroundColor:UIColor.buttonHighlightGreen forState:UIControlStateHighlighted];
[self setBackgroundColor:UIColor.disable forState:UIControlStateDisabled];
}
- (void)setRedAppearance{
[self setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
[self setBackgroundColor:UIColor.buttonNormalRed forState:UIControlStateNormal];
[self setBackgroundColor:UIColor.buttonHighlightRed forState:UIControlStateHighlighted];
[self setBackgroundColor:UIColor.disable forState:UIControlStateDisabled];
}
#pragma mark - Gradient Color
- (void)drawRect:(CGRect)rect{
[super drawRect:rect];
switch (_customType) {
case GMButtonTypeGreenRoundCornerGradient:
_normalGradientColors = @[[UIColor colorWithHex:0x7eefd4],[UIColor colorWithHex:0x66ceff]];
_highlightGradientColors = @[[UIColor colorWithHex:0x34d1c5],[UIColor colorWithHex:0x45BCCF]];
[self setGradientBackgroundColor];
break;
case GMButtonTypeGreenSemicircleGradient:
_normalGradientColors = @[[UIColor colorWithHex:0x45E7DE],[UIColor colorWithHex:0x58CBEF]];
_highlightGradientColors = @[[UIColor colorWithHex:0x2ECCC4],[UIColor colorWithHex:0x42B8DD]];
[self setGradientBackgroundColor];
break;
case GMButtonTypeRedRoundCornerGradient:
case GMButtonTypeRedSemicircleGradient:
_normalGradientColors = @[[UIColor colorWithHex:0xFF6080],[UIColor colorWithHex:0xFA5D66]];
_highlightGradientColors = @[[UIColor colorWithHex:0xE45B7C],[UIColor colorWithHex:0xDE525B]];
[self setGradientBackgroundColor];
break;
default:
break;
}
}
- (UIImage *)gradientColorImageFromColors:(NSArray*)colors gradientType:(GMGradientType)gradientType{
NSMutableArray *ar = [NSMutableArray array];
for(UIColor *c in colors) {
[ar addObject:(id)c.CGColor];
}
UIGraphicsBeginImageContextWithOptions(self.frame.size, YES, 1);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGColorSpaceRef colorSpace = CGColorGetColorSpace([[colors lastObject] CGColor]);
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (CFArrayRef)ar, NULL);
CGPoint start;
CGPoint end;
switch (gradientType) {
case GMGradientTypeTopToBottom:
start = CGPointMake(0.0, 0.0);
end = CGPointMake(0.0,self.frame.size.height);
break;
case GMGradientTypeLeftToRight:
start = CGPointMake(0.0, 0.0);
end = CGPointMake(self.frame.size.width, 0.0);
break;
case GMGradientTypeLeftUpToRightLow:
start = CGPointMake(0.0, 0.0);
end = CGPointMake(self.frame.size.width, self.frame.size.height);
break;
case GMGradientTypeRightUpToLeftLow:
start = CGPointMake(self.frame.size.width, 0.0);
end = CGPointMake(0.0, self.frame.size.height);
break;
default:
break;
}
CGContextDrawLinearGradient(context, gradient, start, end, kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
CGGradientRelease(gradient);
CGContextRestoreGState(context);
CGColorSpaceRelease(colorSpace);
UIGraphicsEndImageContext();
return image;
}
- (void)setGradientBackgroundColor{
UIImage *normalImage = [self gradientColorImageFromColors:_normalGradientColors gradientType:GMGradientTypeLeftUpToRightLow];
UIImage *highlightImage = [self gradientColorImageFromColors:_highlightGradientColors gradientType:GMGradientTypeLeftUpToRightLow];
[self setBackgroundImage:normalImage forState:UIControlStateNormal];
[self setBackgroundImage:highlightImage forState:UIControlStateHighlighted];
[self setBackgroundImage:[UIImage imageWithColor:UIColor.disable] forState:UIControlStateDisabled];
[self setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
}
#pragma mark - Background Color
- (void)setBackgroundColor:(UIColor *)backgroundColor forState:(UIControlState)state{
if (state == UIControlStateNormal) {
_normalBackgroundColor = backgroundColor;
[self setBackgroundColor:_normalBackgroundColor];
}else if (state == UIControlStateHighlighted){
_highlightBackgroundColor = backgroundColor;
}else if (state == UIControlStateSelected){
_selectedBackgroundColor = backgroundColor;
}else if (state == UIControlStateDisabled) {
_disableBackgroundColor = backgroundColor;
}
}
#pragma mark - Setter
- (void)setHighlighted:(BOOL)highlighted{
[super setHighlighted:highlighted];
if (_highlightBackgroundColor && highlighted) {
self.backgroundColor = _highlightBackgroundColor;
}else{
if (self.selected && _selectedBackgroundColor) {
self.backgroundColor = _selectedBackgroundColor;
}else{
self.backgroundColor = _normalBackgroundColor;
}
}
}
- (void)setSelected:(BOOL)selected{
[super setSelected:selected];
if (_selectedBackgroundColor && selected) {
self.backgroundColor = _selectedBackgroundColor;
}else{
self.backgroundColor = _normalBackgroundColor;
}
}
- (void)setBackgroundColor:(UIColor *)backgroundColor{
[super setBackgroundColor:backgroundColor];
// 容错处理,只在为normal状态,才设置_normalBackgroundColor。
// 比如在controller里只调用了setBackgroundColor,为了保证在normal时回复到normal颜色,必须设置好_normalBackgroundColor
if(self.state == UIControlStateNormal){
_normalBackgroundColor = backgroundColor;
}
}
- (void)setEnabled:(BOOL)enabled{
[super setEnabled:enabled];
if (enabled){
self.backgroundColor = _normalBackgroundColor;
switch (_customType) {
case GMButtonTypeGreenSemicircleBorder:
case GMButtonTypeGreenRoundCornerBorder:
case GMButtonTypeGraySemicircleBorder:
case GMButtonTypeRedRoundCornerBorder:
self.layer.borderColor = _normalBorderColor.CGColor;
break;
default:
break;
}
}else{
self.backgroundColor = _disableBackgroundColor;
switch (_customType) {
case GMButtonTypeGreenSemicircleBorder:
case GMButtonTypeGreenRoundCornerBorder:
case GMButtonTypeGraySemicircleBorder:
case GMButtonTypeRedRoundCornerBorder:
self.layer.borderColor = _disableBorderColor.CGColor;
break;
default:
break;
}
}
}
#pragma mark - 支持自适应热区
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event{
//默认不支持放大热区
if (!_enableAdaptive) {
return [super pointInside:point withEvent:event];
}
CGRect bounds = self.bounds;
//若原热区小于 _adaptiveHotAreaWidth x _adaptiveHotAreaHeight,则放大热区,否则保持原大小不变
CGFloat widthDelta = MAX(_adaptiveHotAreaWidth - bounds.size.width, 0);
CGFloat heightDelta = MAX(_adaptiveHotAreaHeight - bounds.size.height, 0);
bounds = CGRectInset(bounds, -0.5 * widthDelta, -0.5 * heightDelta);
return CGRectContainsPoint(bounds, point);
}
@end