JLChen
2021-08-02 38f4fb064df09f344fc3237409c76a9fba2a8a9e
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
//
//  DCRoundSwitch.m
//
//  Created by Patrick Richards on 28/06/11.
//  MIT License.
//
//  http://twitter.com/patr
//  http://domesticcat.com.au/projects
//  http://github.com/domesticcatsoftware/DCRoundSwitch
//
 
#import "DCRoundSwitch.h"
#import "DCRoundSwitchToggleLayer.h"
#import "DCRoundSwitchOutlineLayer.h"
#import "DCRoundSwitchKnobLayer.h"
 
@interface DCRoundSwitch () <UIGestureRecognizerDelegate>
 
@property(nonatomic, strong) DCRoundSwitchOutlineLayer *outlineLayer;
@property(nonatomic, strong) DCRoundSwitchToggleLayer *toggleLayer;
@property(nonatomic, strong) DCRoundSwitchKnobLayer *knobLayer;
@property(nonatomic, strong) CAShapeLayer *clipLayer;
@property (nonatomic, assign) BOOL ignoreTap;
 
- (void)setup;
- (void)useLayerMasking;
- (void)removeLayerMask;
- (void)positionLayersAndMask;
 
@end
 
@implementation DCRoundSwitch
@synthesize outlineLayer, toggleLayer, knobLayer, clipLayer, ignoreTap;
@synthesize on, onText, offText;
@synthesize onTintColor;
 
#pragma mark -
#pragma mark Init & Memory Managment
 
 
- (id)init
{
    if ((self = [super init]))
    {
        self.frame = CGRectMake(0, 0, 77, 27);
        [self setup];
    }
 
    return self;
}
 
- (id)initWithCoder:(NSCoder *)aDecoder
{
    if ((self = [super initWithCoder:aDecoder]))
    {
        [self setup];
    }
 
    return self;
}
 
- (id)initWithFrame:(CGRect)frame
{
    if ((self = [super initWithFrame:frame]))
    {
        [self setup];
    }
 
    return self;
}
 
+ (Class)knobLayerClass {
    return [DCRoundSwitchKnobLayer class];
}
 
+ (Class)outlineLayerClass {
    return [DCRoundSwitchOutlineLayer class];
}
 
+ (Class)toggleLayerClass {
    return [DCRoundSwitchToggleLayer class];
}
 
- (void)setup
{
    // this way you can set the background color to black or something similar so it can be seen in IB
    self.backgroundColor = [UIColor clearColor];
 
    // remove the flexible width/height autoresizing masks if they have been set
    UIViewAutoresizing mask = (int)self.autoresizingMask;
    if (mask & UIViewAutoresizingFlexibleHeight)
        self.autoresizingMask ^= UIViewAutoresizingFlexibleHeight;
 
    if (mask & UIViewAutoresizingFlexibleWidth)
        self.autoresizingMask ^= UIViewAutoresizingFlexibleWidth;
 
    // setup default texts
    NSBundle *uiKitBundle = [NSBundle bundleWithIdentifier:@"com.apple.UIKit"];
    self.onText = uiKitBundle ? [uiKitBundle localizedStringForKey:@"ON" value:nil table:nil] : @"ON";
    self.offText = uiKitBundle ? [uiKitBundle localizedStringForKey:@"OFF" value:nil table:nil] : @"OFF";
 
    // the switch has three layers, (ordered from bottom to top):
    //
    // * toggleLayer * (bottom of the layer stack)
    // this layer contains the onTintColor (blue by default), the text, and the shadown for the knob.  the knob shadow is
    // on this layer because it needs to go under the outlineLayer so it doesn't bleed out over the edge of the control.
    // this layer moves when the switch moves
 
    // * outlineLayer * (middle of the layer stack)
    // this is the outline of the control, it's inner shadow, and the inner gloss.  the inner shadow is on this layer
    // because it must stay still while the switch animates.  the inner gloss is also here because it doesn't move, and also
    // because it needs to go uner the knobLayer.
    // this layer appears to always stay in the same spot.
 
    // * knobLayer * (top of the layer stack)
    // this is the knob, and sits on top of the layer stack. note that the knob shadow is NOT drawn here, it is drawn on the
    // toggleLayer so it doesn't bleed out over the outlineLayer.
 
    self.toggleLayer = [[[[self class] toggleLayerClass] alloc]
        initWithOnString:self.onText
               offString:self.offText
             onTintColor:[UIColor colorWithRed:0.000 green:0.478 blue:0.882 alpha:1.0]];
    self.toggleLayer.drawOnTint = NO;
    self.toggleLayer.clip = YES;
    [self.layer addSublayer:self.toggleLayer];
    [self.toggleLayer setNeedsDisplay];
 
    self.outlineLayer = [[[self class] outlineLayerClass] layer];
    [self.toggleLayer addSublayer:self.outlineLayer];
    [self.outlineLayer setNeedsDisplay];
 
    self.knobLayer = [[[self class] knobLayerClass] layer];
    [self.layer addSublayer:self.knobLayer];
    [self.knobLayer setNeedsDisplay];
 
    self.toggleLayer.contentsScale = self.outlineLayer.contentsScale = self.knobLayer.contentsScale = [[UIScreen mainScreen] scale];
 
    // tap gesture for toggling the switch
    UITapGestureRecognizer *tapGestureRecognizer =
        [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
    [tapGestureRecognizer setDelegate:self];
    [self addGestureRecognizer:tapGestureRecognizer];
 
    // pan gesture for moving the switch knob manually
    UIPanGestureRecognizer *panGestureRecognizer =
        [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(toggleDragged:)];
    [panGestureRecognizer setDelegate:self];
    [self addGestureRecognizer:panGestureRecognizer];
 
    [self setNeedsLayout];
 
    // setup the layer positions
    [self positionLayersAndMask];
}
 
#pragma mark -
#pragma mark Setup Frame/Layout
 
- (void)sizeToFit
{
    [super sizeToFit];
    
    NSString *onString = self.toggleLayer.onString;
    NSString *offString = self.toggleLayer.offString;
 
    CGFloat width = [onString sizeWithFont:self.toggleLayer.labelFont].width;
    CGFloat offWidth = [offString sizeWithFont:self.toggleLayer.labelFont].width;
    
    if(offWidth > width)
        width = offWidth;
    
    width += self.toggleLayer.bounds.size.width * 2.;//add 2x the knob for padding
    
    CGRect newFrame = self.frame;
    CGFloat currentWidth = newFrame.size.width;
    newFrame.size.width = width;
    newFrame.origin.x += currentWidth - width;
    self.frame = newFrame;
 
    //old values for sizeToFit; keep these around for reference
//    newFrame.size.width = 77.0;
//    newFrame.size.height = 27.0;
}
 
- (void)useLayerMasking
{
    // turn of the manual clipping (done in toggleLayer's drawInContext:)
    self.toggleLayer.clip = NO;
    self.toggleLayer.drawOnTint = YES;
    [self.toggleLayer setNeedsDisplay];
 
    // create the layer mask and add that to the toggleLayer
    self.clipLayer = [CAShapeLayer layer];
    UIBezierPath *clipPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
                                                        cornerRadius:self.bounds.size.height / 2.0];
    self.clipLayer.path = clipPath.CGPath;
    self.toggleLayer.mask = self.clipLayer;
}
 
- (void)removeLayerMask
{
    // turn off the animations so the user doesn't see the changing of mask/clipping
    [CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
 
    // remove the layer mask (put on in useLayerMasking)
    self.toggleLayer.mask = nil;
 
    // renable manual clipping (done in toggleLayer's drawInContext:)
    self.toggleLayer.clip = YES;
    self.toggleLayer.drawOnTint = self.on;
    [self.toggleLayer setNeedsDisplay];
}
 
- (void)positionLayersAndMask
{
    // repositions the underlying toggle and the layer mask, plus the knob
    self.toggleLayer.mask.position = CGPointMake(-self.toggleLayer.frame.origin.x, 0.0);
    self.outlineLayer.frame = CGRectMake(-self.toggleLayer.frame.origin.x, 0, self.bounds.size.width, self.bounds.size.height);
    self.knobLayer.frame = CGRectMake(self.toggleLayer.frame.origin.x + self.toggleLayer.frame.size.width / 2.0 - self.knobLayer.frame.size.width / 2.0,
                                 -1,
                                 self.knobLayer.frame.size.width,
                                 self.knobLayer.frame.size.height);
}
 
#pragma mark -
#pragma mark Interaction
 
- (void)tapped:(UITapGestureRecognizer *)gesture
{
    if (self.ignoreTap) return;
    
    if (gesture.state == UIGestureRecognizerStateEnded)
        [self setOn:!self.on animated:YES];
}
 
- (void)toggleDragged:(UIPanGestureRecognizer *)gesture
{
    CGFloat minToggleX = -self.toggleLayer.frame.size.width / 2.0 + self.toggleLayer.frame.size.height / 2.0;
    CGFloat maxToggleX = -1;
 
    if (gesture.state == UIGestureRecognizerStateBegan)
    {
        // setup by turning off the manual clipping of the toggleLayer and setting up a layer mask.
        [CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
        [self useLayerMasking];
        [self positionLayersAndMask];
        self.knobLayer.gripped = YES;
    }
    else if (gesture.state == UIGestureRecognizerStateChanged)
    {
        CGPoint translation = [gesture translationInView:self];
 
        // disable the animations before moving the layers
        [CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
 
        // darken the knob
        if (!self.knobLayer.gripped)
            self.knobLayer.gripped = YES;
 
        // move the toggleLayer using the translation of the gesture, keeping it inside the outline.
        CGFloat newX = self.toggleLayer.frame.origin.x + translation.x;
        if (newX < minToggleX) newX = minToggleX;
        if (newX > maxToggleX) newX = maxToggleX;
        self.toggleLayer.frame = CGRectMake(newX,
                                       self.toggleLayer.frame.origin.y,
                                       self.toggleLayer.frame.size.width,
                                       self.toggleLayer.frame.size.height);
 
        // this will re-position the layer mask and knob
        [self positionLayersAndMask];
 
        [gesture setTranslation:CGPointZero inView:self];
    }
    else if (gesture.state == UIGestureRecognizerStateEnded)
    {
        // flip the switch to on or off depending on which half it ends at
        CGFloat toggleCenter = CGRectGetMidX(self.toggleLayer.frame);
        [self setOn:(toggleCenter > CGRectGetMidX(self.bounds)) animated:YES];
    }
 
    // send off the appropriate actions (not fully tested yet)
    CGPoint locationOfTouch = [gesture locationInView:self];
    if (CGRectContainsPoint(self.bounds, locationOfTouch))
        [self sendActionsForControlEvents:UIControlEventTouchDragInside];
    else
        [self sendActionsForControlEvents:UIControlEventTouchDragOutside];
}
 
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (self.ignoreTap) return;
 
    [super touchesBegan:touches withEvent:event];
 
    self.knobLayer.gripped = YES;
    [self sendActionsForControlEvents:UIControlEventTouchDown];
}
 
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesEnded:touches withEvent:event];
 
    [self sendActionsForControlEvents:UIControlEventTouchUpInside];
}
 
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesCancelled:touches withEvent:event];
 
    [self sendActionsForControlEvents:UIControlEventTouchUpOutside];
}
 
#pragma mark UIGestureRecognizerDelegate
 
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer;
{
    return !self.ignoreTap;
}
 
#pragma mark Setters/Getters
 
- (void)setOn:(BOOL)newOn
{
    [self setOn:newOn animated:NO];
}
 
- (void)setOn:(BOOL)newOn animated:(BOOL)animated
{
    [self setOn:newOn animated:animated ignoreControlEvents:NO];
}
 
- (void)setOn:(BOOL)newOn animated:(BOOL)animated ignoreControlEvents:(BOOL)ignoreControlEvents
{
    BOOL previousOn = self.on;
    on = newOn;
    self.ignoreTap = YES;
 
    [CATransaction setAnimationDuration:0.014];
    self.knobLayer.gripped = YES;
 
    // setup by turning off the manual clipping of the toggleLayer and setting up a layer mask.
    [self useLayerMasking];
    [self positionLayersAndMask];
 
    // retain all our targets so they don't disappear before the actions get sent at the end of the animation
 
    [CATransaction setCompletionBlock:^{
        [CATransaction begin];
        if (!animated)
            [CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
        else
            [CATransaction setValue:(id)kCFBooleanFalse forKey:kCATransactionDisableActions];
 
        CGFloat minToggleX = -self.toggleLayer.frame.size.width / 2.0 + self.toggleLayer.frame.size.height / 2.0;
        CGFloat maxToggleX = -1;
 
 
        if (self.on)
        {
            self.toggleLayer.frame = CGRectMake(maxToggleX,
                                           self.toggleLayer.frame.origin.y,
                                           self.toggleLayer.frame.size.width,
                                           self.toggleLayer.frame.size.height);
        }
        else
        {
            self.toggleLayer.frame = CGRectMake(minToggleX,
                                           self.toggleLayer.frame.origin.y,
                                           self.toggleLayer.frame.size.width,
                                           self.toggleLayer.frame.size.height);
        }
 
        if (!self.toggleLayer.mask)
        {
            [self useLayerMasking];
            [self.toggleLayer setNeedsDisplay];
        }
 
        [self positionLayersAndMask];
 
        self.knobLayer.gripped = NO;
 
        [CATransaction setCompletionBlock:^{
            [self removeLayerMask];
            self.ignoreTap = NO;
 
            // send the action here so it get's sent at the end of the animations
            if (previousOn != on && !ignoreControlEvents)
                [self sendActionsForControlEvents:UIControlEventValueChanged];
 
        }];
 
        [CATransaction commit];
    }];
}
 
- (void)setOnTintColor:(UIColor *)anOnTintColor
{
    if (anOnTintColor != onTintColor)
    {
        onTintColor = anOnTintColor;
        self.toggleLayer.onTintColor = anOnTintColor;
        [self.toggleLayer setNeedsDisplay];
    }
}
 
- (void)layoutSubviews;
{
    CGFloat knobRadius = self.bounds.size.height + 2.0;
    self.knobLayer.frame = CGRectMake(0, 0, knobRadius, knobRadius);
    CGSize toggleSize = CGSizeMake(self.bounds.size.width * 2 - (knobRadius - 4), self.bounds.size.height);
    CGFloat minToggleX = -toggleSize.width / 2.0 + knobRadius / 2.0 - 1;
    CGFloat maxToggleX = -1;
 
    if (self.on)
    {
        self.toggleLayer.frame = CGRectMake(maxToggleX,
                                       self.toggleLayer.frame.origin.y,
                                       toggleSize.width,
                                       toggleSize.height);
    }
    else
    {
        self.toggleLayer.frame = CGRectMake(minToggleX,
                                       self.toggleLayer.frame.origin.y,
                                       toggleSize.width,
                                       toggleSize.height);
    }
 
    [self positionLayersAndMask];
}
 
- (void)setOnText:(NSString *)newOnText
{
    if (newOnText != onText)
    {
        onText = [newOnText copy];
        self.toggleLayer.onString = onText;
        [self.toggleLayer setNeedsDisplay];
    }
}
 
- (void)setOffText:(NSString *)newOffText
{
    if (newOffText != offText)
    {
        offText = [newOffText copy];
        self.toggleLayer.offString = offText;
        [self.toggleLayer setNeedsDisplay];
    }
}
 
@end