-
Notifications
You must be signed in to change notification settings - Fork 42
/
FSSwitchButton.m
188 lines (168 loc) · 7.37 KB
/
FSSwitchButton.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
#import "FSSwitchButton.h"
#import "FSSwitchPanel.h"
#import "NSBundle+Flipswitch.h"
@interface CALayer (Private)
@property (assign) BOOL allowsGroupBlending;
@end
#define HAS_GESTURE_RECOGNIZER (kCFCoreFoundationVersionNumber >= 478.61)
@implementation _FSSwitchButton
- (id)initWithSwitchIdentifier:(NSString *)switchIdentifier_ template:(NSBundle *)template_
{
NSDictionary *infoDictionary = template_.flipswitchThemedInfoDictionary;
CGFloat width = [[infoDictionary objectForKey:@"width"] floatValue];
CGFloat height = [[infoDictionary objectForKey:@"height"] floatValue];
if ((self = [super initWithFrame:CGRectMake(0.0f, 0.0f, width, height)])) {
switchIdentifier = [switchIdentifier_ copy];
template = [template_ retain];
self.adjustsImageWhenHighlighted = NO;
self.adjustsImageWhenDisabled = NO;
CALayer *layer = self.layer;
layer.needsDisplayOnBoundsChange = NO;
if ([layer respondsToSelector:@selector(setAllowsGroupBlending:)]) {
[layer setAllowsGroupBlending:NO];
}
[layer setNeedsDisplay];
[self addObserver:self forKeyPath:@"enabled" options:0 context:template];
[self addObserver:self forKeyPath:@"highlighted" options:0 context:template];
[self addObserver:self forKeyPath:@"selected" options:0 context:template];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(switchStateDidChange:) name:FSSwitchPanelSwitchStateChangedNotification object:nil];
[self addTarget:self action:@selector(_pressed) forControlEvents:UIControlEventTouchUpInside];
self.enabled = [[FSSwitchPanel sharedPanel] switchWithIdentifierIsEnabled:switchIdentifier_];
if (HAS_GESTURE_RECOGNIZER) {
UILongPressGestureRecognizer *recognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(_longPressRecognizerUpdated:)];
[self addGestureRecognizer:recognizer];
[recognizer release];
}
}
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[self removeObserver:self forKeyPath:@"enabled"];
[self removeObserver:self forKeyPath:@"highlighted"];
[self removeObserver:self forKeyPath:@"selected"];
[switchIdentifier release];
[template release];
[backgroundView release];
[currentBackgroundImage release];
[super dealloc];
}
- (NSString *)description
{
NSString *addedContent = [NSString stringWithFormat:@" switchIdentifier=%@ template=%@", switchIdentifier, template.bundleIdentifier ?: template.bundlePath];
NSMutableString *result = [[[super description] mutableCopy] autorelease];
[result insertString:addedContent atIndex:[result length] - 1];
return result;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if (context != template)
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
else
[self.layer setNeedsDisplay];
}
- (void)switchStateDidChange:(NSNotification *)notification
{
NSString *changedIdentifier = [notification.userInfo objectForKey:FSSwitchPanelSwitchIdentifierKey];
if ([changedIdentifier isEqual:switchIdentifier] || !changedIdentifier) {
self.enabled = [[FSSwitchPanel sharedPanel] switchWithIdentifierIsEnabled:switchIdentifier];
[self.layer setNeedsDisplay];
}
}
- (void)displayLayer:(CALayer *)layer
{
FSSwitchPanel *sharedPanel = [FSSwitchPanel sharedPanel];
FSSwitchState switchState = [sharedPanel stateForSwitchIdentifier:switchIdentifier];
UIControlState controlState = self.state;
UIImage *image = [sharedPanel imageOfSwitchState:switchState controlState:controlState forSwitchIdentifier:switchIdentifier usingTemplate:template];
[self setImage:image forState:UIControlStateNormal];
[self setImage:image forState:UIControlStateHighlighted];
UIImageView *imageView = self.imageView;
[sharedPanel applyEffectsToLayer:imageView.layer forSwitchState:switchState controlState:controlState usingTemplate:template];
// Add background image so that button can be stretchable
UIImage *backgroundImage = [sharedPanel imageOfSwitchState:switchState controlState:controlState forSwitchIdentifier:switchIdentifier usingLayerSet:@"background" inTemplate:template];
if (backgroundImage && (backgroundImage != currentBackgroundImage)) {
[currentBackgroundImage release];
currentBackgroundImage = [backgroundImage retain];
CGSize size = backgroundImage.size;
if ([backgroundImage respondsToSelector:@selector(resizableImageWithCapInsets:resizingMode:)]) {
backgroundImage = [backgroundImage resizableImageWithCapInsets:UIEdgeInsetsMake(size.height * 0.5, size.width * 0.5, size.height * 0.5, size.width * 0.5) resizingMode:UIImageResizingModeStretch];
} else if ([backgroundImage respondsToSelector:@selector(resizableImageWithCapInsets:)]) {
backgroundImage = [backgroundImage resizableImageWithCapInsets:UIEdgeInsetsMake(size.height * 0.5, size.width * 0.5, size.height * 0.5, size.width * 0.5)];
} else {
backgroundImage = [backgroundImage stretchableImageWithLeftCapWidth:size.width * 0.5 topCapHeight:size.height * 0.5];
}
if (!backgroundView) {
backgroundView = [[UIImageView alloc] initWithFrame:self.bounds];
backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[imageView.superview insertSubview:backgroundView belowSubview:imageView];
}
backgroundView.image = backgroundImage;
[sharedPanel applyEffectsToLayer:backgroundView.layer forSwitchState:switchState controlState:controlState usingLayerSet:@"background" inTemplate:template];
}
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
if (!HAS_GESTURE_RECOGNIZER) {
skippingForHold = NO;
[self performSelector:@selector(_held) withObject:nil afterDelay:0.5];
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesMoved:touches withEvent:event];
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(_held) object:nil];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesEnded:touches withEvent:event];
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(_held) object:nil];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesCancelled:touches withEvent:event];
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(_held) object:nil];
}
- (void)_pressed
{
if (!skippingForHold) {
[[FSSwitchPanel sharedPanel] applyActionForSwitchIdentifier:switchIdentifier];
}
}
- (void)_held
{
skippingForHold = YES;
[[FSSwitchPanel sharedPanel] applyAlternateActionForSwitchIdentifier:switchIdentifier];
}
- (void)_longPressRecognizerUpdated:(UILongPressGestureRecognizer *)recognizer
{
if (recognizer.state == UIGestureRecognizerStateBegan) {
[[FSSwitchPanel sharedPanel] applyAlternateActionForSwitchIdentifier:switchIdentifier];
}
}
- (NSString *)accessibilityLabel
{
return [[FSSwitchPanel sharedPanel] titleForSwitchIdentifier:switchIdentifier];
}
- (NSString *)accessibilityValue
{
switch ([[FSSwitchPanel sharedPanel] stateForSwitchIdentifier:switchIdentifier]) {
case FSSwitchStateOff:
return @"Off";
case FSSwitchStateOn:
return @"On";
default:
return nil;
}
}
- (void)didMoveToWindow
{
[super didMoveToWindow];
if (self.window)
[[FSSwitchPanel sharedPanel] beginPrewarmingForSwitchIdentifier:switchIdentifier];
else
[[FSSwitchPanel sharedPanel] cancelPrewarmingForSwitchIdentifier:switchIdentifier];
}
@end