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
//
// KWMButton.m
// iOrangeBusiness
//
// Created by Yaotian on 7/3/15.
// Copyright (c) 2015 kwm. All rights reserved.
//
#import "KWMButton.h"
@implementation KWMButton
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self setup];
}
return self;
}
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self setup];
}
return self;
}
- (void)setup {
[self pressedColor];
[self normalColor];
[self addTarget:self action:@selector(wasPressed) forControlEvents:(UIControlEventTouchDown |
UIControlEventTouchDownRepeat |
UIControlEventTouchDragInside |
UIControlEventTouchDragEnter)];
[self addTarget:self action:@selector(endedPress) forControlEvents:(UIControlEventTouchCancel |
UIControlEventTouchDragOutside |
UIControlEventTouchDragExit |
UIControlEventTouchUpInside |
UIControlEventTouchUpOutside)];
}
- (void)wasPressed {
self.backgroundColor = self.pressedColor;
}
- (void)endedPress {
self.backgroundColor = self.normalColor;
}
- (UIColor *)pressedColor {
if (!_pressedColor) {
static CGFloat kHighlightDelta = 0.08;
CGFloat red, grn, blu, white, alpha = 0.0;
[self.backgroundColor getRed:&red green:&grn blue:&blu alpha:&alpha];
[self.backgroundColor getWhite:&white alpha:&alpha];
_pressedColor = [UIColor colorWithRed:red - kHighlightDelta
green:grn - kHighlightDelta
blue:blu - kHighlightDelta
alpha:alpha];
}
return _pressedColor;
}
- (UIColor *)normalColor {
if (!_normalColor) {
_normalColor = self.backgroundColor;
}
return _normalColor;
}
@end