forked from bvogelzang/SevenSwitch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SevenSwitch.m
549 lines (468 loc) · 16.4 KB
/
SevenSwitch.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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
//
// SevenSwitch
//
// Created by Benjamin Vogelzang on 6/10/13.
// Copyright (c) 2013 Ben Vogelzang. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// Initial version written by Benjamin Vogelzang
// https://github.com/bvogelzang/SevenSwitch
//
// Modified to add single text label on the switch by Michael Henrey:
// https://github.com/michaelhenry/SevenSwitch
//
// Modified by MShahin to allow for onText and offText
#import "SevenSwitch.h"
#import <QuartzCore/QuartzCore.h>
@interface SevenSwitch () {
UIView *background;
UIView *knob;
UIImageView *onImageView;
UIImageView *offImageView;
UILabel * textLabel;
CGRect inactiveTextFrame;
CGRect activeTextFrame;
BOOL currentVisualValue;
BOOL startTrackingValue;
BOOL didChangeWhileTracking;
BOOL isAnimating;
UIPanGestureRecognizer * _panGestureRecognizer;
}
- (void)showOn:(BOOL)animated;
- (void)showOff:(BOOL)animated;
- (void)setup;
@end
@interface UIColor(LightAndDark)
- (UIColor *)lighterColor;
- (UIColor *)darkerColor;
@end
@implementation UIColor (LightAndDark)
- (UIColor *)lighterColor
{
CGFloat h, s, b, a;
if ([self getHue:&h saturation:&s brightness:&b alpha:&a])
return [UIColor colorWithHue:h
saturation:s
brightness:MIN(b * 1.3, 1.0)
alpha:a];
return nil;
}
- (UIColor *)darkerColor
{
CGFloat h, s, b, a;
if ([self getHue:&h saturation:&s brightness:&b alpha:&a])
return [UIColor colorWithHue:h
saturation:s
brightness:b * 0.75
alpha:a];
return nil;
}
@end
@implementation SevenSwitch
@synthesize inactiveColor, activeColor, onTintColor, borderColor, thumbTintColor, shadowColor;
@synthesize onImage, offImage;
@synthesize isRounded;
@synthesize on;
@synthesize textActiveColor;
@synthesize textInActiveColor;
#pragma mark init Methods
- (id)init {
self = [super initWithFrame:CGRectMake(0, 0, 50, 30)];
if (self) {
[self setup];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self setup];
}
return self;
}
- (id)initWithFrame:(CGRect)frame
{
// use the default values if CGRectZero frame is set
CGRect initialFrame;
if (CGRectIsEmpty(frame)) {
initialFrame = CGRectMake(0, 0, 50, 30);
}
else {
initialFrame = frame;
}
self = [super initWithFrame:initialFrame];
if (self) {
[self setup];
}
return self;
}
/**
* Setup the individual elements of the switch and set default values
*/
- (void)setup {
// default values
self.on = NO;
self.isRounded = YES;
self.inactiveColor = [UIColor clearColor];
self.activeColor = [UIColor colorWithRed:0.89f green:0.89f blue:0.89f alpha:1.00f];
self.onTintColor = [UIColor colorWithRed:0.30f green:0.85f blue:0.39f alpha:1.00f];
self.borderColor = [UIColor colorWithRed:0.89f green:0.89f blue:0.91f alpha:1.00f];
self.thumbTintColor = [UIColor whiteColor];
self.shadowColor = [UIColor grayColor];
currentVisualValue = NO;
// background
background = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
background.backgroundColor = [UIColor clearColor];
background.layer.cornerRadius = self.frame.size.height * 0.5;
background.layer.borderColor = self.borderColor.CGColor;
background.layer.borderWidth = 1.0;
background.userInteractionEnabled = NO;
[self addSubview:background];
// images
onImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width - self.frame.size.height, self.frame.size.height)];
onImageView.alpha = 0;
onImageView.contentMode = UIViewContentModeCenter;
[self addSubview:onImageView];
offImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.height, 0, self.frame.size.width - self.frame.size.height, self.frame.size.height)];
offImageView.alpha = 1.0;
offImageView.contentMode = UIViewContentModeCenter;
[self addSubview:offImageView];
// knob
knob = [[UIView alloc] initWithFrame:CGRectMake(1, 1, self.frame.size.height - 2, self.frame.size.height - 2)];
knob.backgroundColor = self.thumbTintColor;
knob.layer.cornerRadius = (self.frame.size.height * 0.5) - 1;
knob.layer.shadowColor = self.shadowColor.CGColor;
knob.layer.shadowRadius = 2.0;
knob.layer.shadowOpacity = 0.5;
knob.layer.shadowOffset = CGSizeMake(0, 3);
knob.layer.masksToBounds = NO;
knob.userInteractionEnabled = NO;
inactiveTextFrame = CGRectMake(self.frame.size.height + 6.0f
, 0.0f
, self.frame.size.width-10.0f
, self.frame.size.height);
activeTextFrame = CGRectMake(10.0f
, 0.0f
, self.frame.size.width-10.0f
, self.frame.size.height);
textLabel = [[UILabel alloc]initWithFrame:inactiveTextFrame];
textLabel.backgroundColor = [UIColor clearColor];
textLabel.font = [UIFont systemFontOfSize:12.0f];
textLabel.userInteractionEnabled = NO;
[self addSubview:textLabel];
[self addSubview:knob];
_panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
[self addGestureRecognizer:_panGestureRecognizer];
isAnimating = NO;
}
- (void) handleSwipe:(UIPanGestureRecognizer*) gesture {
CGPoint translation = [gesture translationInView:self];
if (gesture.state == UIGestureRecognizerStateChanged)
{
if(translation.x > self.frame.size.width/4.0f) {
if(!self.isOn) {
[self setOn:YES animated:YES];
[self sendActionsForControlEvents:UIControlEventValueChanged];
}
}else if(translation.x < (-self.frame.size.width/4.0f)) {
if(self.isOn) {
[self setOn:NO animated:YES];
[self sendActionsForControlEvents:UIControlEventValueChanged];
}
}
}
}
#pragma mark Touch Tracking
- (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
[super beginTrackingWithTouch:touch withEvent:event];
startTrackingValue = self.on;
didChangeWhileTracking = NO;
// make the knob larger and animate to the correct color
CGFloat activeKnobWidth = self.bounds.size.height - 2 + 5;
isAnimating = YES;
[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveEaseOut|UIViewAnimationOptionBeginFromCurrentState animations:^{
if (self.on) {
knob.frame = CGRectMake(self.bounds.size.width - (activeKnobWidth + 1), knob.frame.origin.y, activeKnobWidth, knob.frame.size.height);
background.backgroundColor = self.onTintColor;
textLabel.textColor = [self.onTintColor lighterColor];
}
else {
knob.frame = CGRectMake(knob.frame.origin.x, knob.frame.origin.y, activeKnobWidth, knob.frame.size.height);
background.backgroundColor = self.activeColor;
textLabel.textColor = [self.activeColor lighterColor];
}
} completion:^(BOOL finished) {
isAnimating = NO;
}];
return YES;
}
- (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
[super continueTrackingWithTouch:touch withEvent:event];
// Get touch location
CGPoint lastPoint = [touch locationInView:self];
// update the switch to the correct visuals depending on if
// they moved their touch to the right or left side of the switch
if (lastPoint.x > self.bounds.size.width * 0.5) {
[self showOn:YES];
if (!startTrackingValue) {
didChangeWhileTracking = YES;
}
}
else {
[self showOff:YES];
if (startTrackingValue) {
didChangeWhileTracking = YES;
}
}
return YES;
}
- (void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event {
[super endTrackingWithTouch:touch withEvent:event];
BOOL previousValue = self.on;
if (didChangeWhileTracking) {
[self setOn:currentVisualValue animated:YES];
}
else {
[self setOn:!self.on animated:YES];
}
if (previousValue != self.on)
[self sendActionsForControlEvents:UIControlEventValueChanged];
}
- (void)cancelTrackingWithEvent:(UIEvent *)event {
[super cancelTrackingWithEvent:event];
// just animate back to the original value
if (self.on)
[self showOn:YES];
else
[self showOff:YES];
}
- (void)layoutSubviews {
[super layoutSubviews];
if (!isAnimating) {
CGRect frame = self.frame;
if (self.isOn)
textLabel.textColor = self.textActiveColor;
else
textLabel.textColor = self.textInActiveColor;
// background
background.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
background.layer.cornerRadius = self.isRounded ? frame.size.height * 0.5 : 2;
// images
onImageView.frame = CGRectMake(0, 0, frame.size.width - frame.size.height, frame.size.height);
offImageView.frame = CGRectMake(frame.size.height, 0, frame.size.width - frame.size.height, frame.size.height);
// knob
CGFloat normalKnobWidth = frame.size.height - 2;
if (self.on)
knob.frame = CGRectMake(frame.size.width - (normalKnobWidth + 1), 1, frame.size.height - 2, normalKnobWidth);
else
knob.frame = CGRectMake(1, 1, normalKnobWidth, normalKnobWidth);
knob.layer.cornerRadius = self.isRounded ? (frame.size.height * 0.5) - 1 : 2;
}
}
#pragma mark Setters
/*
* Sets the background color when the switch is off.
* Defaults to clear color.
*/
- (void)setInactiveColor:(UIColor *)color {
inactiveColor = color;
if (!self.on && !self.isTracking)
background.backgroundColor = color;
}
/*
* Sets the background color that shows when the switch is on.
* Defaults to green.
*/
- (void)setOnTintColor:(UIColor *)color {
onTintColor = color;
if (self.on && !self.isTracking) {
background.backgroundColor = color;
background.layer.borderColor = color.CGColor;
}
}
/*
* Sets the border color that shows when the switch is off. Defaults to light gray.
*/
- (void)setBorderColor:(UIColor *)color {
borderColor = color;
if (!self.on)
background.layer.borderColor = color.CGColor;
}
/*
* Sets the shadow color of the knob. Defaults to gray.
*/
- (void)setShadowColor:(UIColor *)color {
shadowColor = color;
knob.layer.shadowColor = color.CGColor;
}
/*
* Sets the image that shows when the switch is on.
* The image is centered in the area not covered by the knob.
* Make sure to size your images appropriately.
*/
- (void)setOnImage:(UIImage *)image {
onImage = image;
onImageView.image = image;
}
/*
* Sets the image that shows when the switch is off.
* The image is centered in the area not covered by the knob.
* Make sure to size your images appropriately.
*/
- (void)setOffImage:(UIImage *)image {
offImage = image;
offImageView.image = image;
}
/*
* Sets whether or not the switch edges are rounded.
* Set to NO to get a stylish square switch.
* Defaults to YES.
*/
- (void)setIsRounded:(BOOL)rounded {
isRounded = rounded;
if (rounded) {
background.layer.cornerRadius = self.frame.size.height * 0.5;
knob.layer.cornerRadius = (self.frame.size.height * 0.5) - 1;
}
else {
background.layer.cornerRadius = 2;
knob.layer.cornerRadius = 2;
}
}
/*
* Set (without animation) whether the switch is on or off
*/
- (void)setOn:(BOOL)isOn {
[self setOn:isOn animated:NO];
}
/*
* Set the state of the switch to on or off, optionally animating the transition.
*/
- (void)setOn:(BOOL)isOn animated:(BOOL)animated {
on = isOn;
if (isOn) {
[self showOn:animated];
}
else {
[self showOff:animated];
}
}
#pragma mark Getters
/*
* Detects whether the switch is on or off
*
* @return BOOL YES if switch is on. NO if switch is off
*/
- (BOOL)isOn {
return self.on;
}
#pragma mark State Changes
/*
* update the looks of the switch to be in the on position
* optionally make it animated
*/
- (void)showOn:(BOOL)animated {
CGFloat normalKnobWidth = self.bounds.size.height - 2;
CGFloat activeKnobWidth = normalKnobWidth + 5;
if (animated) {
isAnimating = YES;
[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveEaseOut|UIViewAnimationOptionBeginFromCurrentState animations:^{
if (self.tracking)
knob.frame = CGRectMake(self.bounds.size.width - (activeKnobWidth + 1), knob.frame.origin.y, activeKnobWidth, knob.frame.size.height);
else
knob.frame = CGRectMake(self.bounds.size.width - (normalKnobWidth + 1), knob.frame.origin.y, normalKnobWidth, knob.frame.size.height);
background.backgroundColor = self.onTintColor;
background.layer.borderColor = self.onTintColor.CGColor;
onImageView.alpha = 1.0;
offImageView.alpha = 0;
textLabel.text = self.onText;
textLabel.textColor = self.textActiveColor;
textLabel.frame = activeTextFrame;
} completion:^(BOOL finished) {
isAnimating = NO;
}];
}
else {
if (self.tracking)
knob.frame = CGRectMake(self.bounds.size.width - (activeKnobWidth + 1), knob.frame.origin.y, activeKnobWidth, knob.frame.size.height);
else
knob.frame = CGRectMake(self.bounds.size.width - (normalKnobWidth + 1), knob.frame.origin.y, normalKnobWidth, knob.frame.size.height);
background.backgroundColor = self.onTintColor;
background.layer.borderColor = self.onTintColor.CGColor;
textLabel.text = self.onText;
textLabel.textColor = self.textActiveColor;
textLabel.frame = activeTextFrame;
onImageView.alpha = 1.0;
offImageView.alpha = 0;
}
currentVisualValue = YES;
}
/*
* update the looks of the switch to be in the off position
* optionally make it animated
*/
- (void)showOff:(BOOL)animated {
CGFloat normalKnobWidth = self.bounds.size.height - 2;
CGFloat activeKnobWidth = normalKnobWidth + 5;
if (animated) {
isAnimating = YES;
[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveEaseOut|UIViewAnimationOptionBeginFromCurrentState animations:^{
if (self.tracking) {
knob.frame = CGRectMake(1, knob.frame.origin.y, activeKnobWidth, knob.frame.size.height);
background.backgroundColor = self.activeColor;
textLabel.text = self.offText;
textLabel.textColor = self.textActiveColor;
textLabel.frame = activeTextFrame;
}
else {
knob.frame = CGRectMake(1, knob.frame.origin.y, normalKnobWidth, knob.frame.size.height);
background.backgroundColor = self.inactiveColor;
textLabel.text = self.offText;
textLabel.textColor = self.textInActiveColor;
textLabel.frame = inactiveTextFrame;
}
background.layer.borderColor = self.borderColor.CGColor;
onImageView.alpha = 0;
offImageView.alpha = 1.0;
} completion:^(BOOL finished) {
isAnimating = NO;
}];
}
else {
if (self.tracking) {
knob.frame = CGRectMake(1, knob.frame.origin.y, activeKnobWidth, knob.frame.size.height);
background.backgroundColor = self.activeColor;
textLabel.text = self.offText;
textLabel.textColor = self.textActiveColor;
textLabel.frame = activeTextFrame;
}
else {
knob.frame = CGRectMake(1, knob.frame.origin.y, normalKnobWidth, knob.frame.size.height);
background.backgroundColor = self.inactiveColor;
textLabel.text = self.offText;
textLabel.textColor = self.textInActiveColor;
textLabel.frame = inactiveTextFrame;
}
background.layer.borderColor = self.borderColor.CGColor;
onImageView.alpha = 0;
offImageView.alpha = 1.0;
}
currentVisualValue = NO;
}
@end