-
Notifications
You must be signed in to change notification settings - Fork 193
/
SevenSwitch.swift
543 lines (471 loc) · 21.4 KB
/
SevenSwitch.swift
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
//
// SevenSwitch.swift
//
// Created by Benjamin Vogelzang on 6/20/14.
// Copyright (c) 2014 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.
import UIKit
import QuartzCore
@IBDesignable @objc open class SevenSwitch: UIControl {
// public
/*
* Set (without animation) whether the switch is on or off
*/
@IBInspectable open var on: Bool {
get {
return switchValue
}
set {
switchValue = newValue
self.setOn(newValue, animated: false)
}
}
/*
* Sets the background color that shows when the switch off and actively being touched.
* Defaults to light gray.
*/
@IBInspectable open var activeColor: UIColor = UIColor(red: 0.89, green: 0.89, blue: 0.89, alpha: 1) {
willSet {
if self.on && !self.isTracking {
backgroundView.backgroundColor = newValue
}
}
}
/*
* Sets the background color when the switch is off.
* Defaults to clear color.
*/
@IBInspectable open var inactiveColor: UIColor = UIColor.clear {
willSet {
if !self.on && !self.isTracking {
backgroundView.backgroundColor = newValue
}
}
}
/*
* Sets the background color that shows when the switch is on.
* Defaults to green.
*/
@IBInspectable open var onTintColor: UIColor = UIColor(red: 0.3, green: 0.85, blue: 0.39, alpha: 1) {
willSet {
if self.on && !self.isTracking {
backgroundView.backgroundColor = newValue
backgroundView.layer.borderColor = newValue.cgColor
}
}
}
/*
* Sets the border color that shows when the switch is off. Defaults to light gray.
*/
@IBInspectable open var borderColor: UIColor = UIColor(red: 0.78, green: 0.78, blue: 0.8, alpha: 1) {
willSet {
if !self.on {
backgroundView.layer.borderColor = newValue.cgColor
}
}
}
/*
* Sets the knob color. Defaults to white.
*/
@IBInspectable open var thumbTintColor: UIColor = UIColor.white {
willSet {
if !userDidSpecifyOnThumbTintColor {
onThumbTintColor = newValue
}
if (!userDidSpecifyOnThumbTintColor || !self.on) && !self.isTracking {
thumbView.backgroundColor = newValue
}
}
}
/*
* Sets the knob color that shows when the switch is on. Defaults to white.
*/
@IBInspectable open var onThumbTintColor: UIColor = UIColor.white {
willSet {
userDidSpecifyOnThumbTintColor = true
if self.on && !self.isTracking {
thumbView.backgroundColor = newValue
}
}
}
/*
* Sets the shadow color of the knob. Defaults to gray.
*/
@IBInspectable open var shadowColor: UIColor = UIColor.gray {
willSet {
thumbView.layer.shadowColor = newValue.cgColor
}
}
/*
* Sets whether or not the switch edges are rounded.
* Set to NO to get a stylish square switch.
* Defaults to YES.
*/
@IBInspectable open var isRounded: Bool = true {
willSet {
if newValue {
backgroundView.layer.cornerRadius = self.frame.size.height * 0.5
thumbView.layer.cornerRadius = (self.frame.size.height * 0.5) - 1
}
else {
backgroundView.layer.cornerRadius = 2
thumbView.layer.cornerRadius = 2
}
thumbView.layer.shadowPath = UIBezierPath(roundedRect: thumbView.bounds, cornerRadius: thumbView.layer.cornerRadius).cgPath
}
}
/*
* Sets the image that shows on the switch thumb.
*/
@IBInspectable open var thumbImage: UIImage! {
willSet {
thumbImageView.image = newValue
}
}
/*
* 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.
*/
@IBInspectable open var onImage: UIImage! {
willSet {
onImageView.image = newValue
}
}
/*
* 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.
*/
@IBInspectable open var offImage: UIImage! {
willSet {
offImageView.image = newValue
}
}
/*
* Sets the text that shows when the switch is on.
* The text is centered in the area not covered by the knob.
*/
open var onLabel: UILabel!
/*
* Sets the text that shows when the switch is off.
* The text is centered in the area not covered by the knob.
*/
open var offLabel: UILabel!
// internal
internal var backgroundView: UIView!
internal var thumbView: UIView!
internal var onImageView: UIImageView!
internal var offImageView: UIImageView!
internal var thumbImageView: UIImageView!
// private
fileprivate var currentVisualValue: Bool = false
fileprivate var startTrackingValue: Bool = false
fileprivate var didChangeWhileTracking: Bool = false
fileprivate var isAnimating: Bool = false
fileprivate var userDidSpecifyOnThumbTintColor: Bool = false
fileprivate var switchValue: Bool = false
/*
* Initialization
*/
public convenience init() {
self.init(frame: CGRect(x: 0, y: 0, width: 50, height: 30))
}
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.setup()
}
override public init(frame: CGRect) {
let initialFrame = frame.isEmpty ? CGRect(x: 0, y: 0, width: 50, height: 30) : frame
super.init(frame: initialFrame)
self.setup()
}
/*
* Setup the individual elements of the switch and set default values
*/
fileprivate func setup() {
// background
self.backgroundView = UIView(frame: CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height))
backgroundView.backgroundColor = UIColor.clear
backgroundView.layer.cornerRadius = self.frame.size.height * 0.5
backgroundView.layer.borderColor = self.borderColor.cgColor
backgroundView.layer.borderWidth = 1.0
backgroundView.isUserInteractionEnabled = false
backgroundView.clipsToBounds = true
self.addSubview(backgroundView)
// on/off images
self.onImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: self.frame.size.width - self.frame.size.height, height: self.frame.size.height))
onImageView.alpha = 1.0
onImageView.contentMode = UIViewContentMode.center
backgroundView.addSubview(onImageView)
self.offImageView = UIImageView(frame: CGRect(x: self.frame.size.height, y: 0, width: self.frame.size.width - self.frame.size.height, height: self.frame.size.height))
offImageView.alpha = 1.0
offImageView.contentMode = UIViewContentMode.center
backgroundView.addSubview(offImageView)
// labels
self.onLabel = UILabel(frame: CGRect(x: 0, y: 0, width: self.frame.size.width - self.frame.size.height, height: self.frame.size.height))
onLabel.textAlignment = NSTextAlignment.center
onLabel.textColor = UIColor.lightGray
onLabel.font = UIFont.systemFont(ofSize: 12)
backgroundView.addSubview(onLabel)
self.offLabel = UILabel(frame: CGRect(x: self.frame.size.height, y: 0, width: self.frame.size.width - self.frame.size.height, height: self.frame.size.height))
offLabel.textAlignment = NSTextAlignment.center
offLabel.textColor = UIColor.lightGray
offLabel.font = UIFont.systemFont(ofSize: 12)
backgroundView.addSubview(offLabel)
// thumb
self.thumbView = UIView(frame: CGRect(x: 1, y: 1, width: self.frame.size.height - 2, height: self.frame.size.height - 2))
thumbView.backgroundColor = self.thumbTintColor
thumbView.layer.cornerRadius = (self.frame.size.height * 0.5) - 1
thumbView.layer.shadowColor = self.shadowColor.cgColor
thumbView.layer.shadowRadius = 2.0
thumbView.layer.shadowOpacity = 0.5
thumbView.layer.shadowOffset = CGSize(width: 0, height: 3)
thumbView.layer.shadowPath = UIBezierPath(roundedRect: thumbView.bounds, cornerRadius: thumbView.layer.cornerRadius).cgPath
thumbView.layer.masksToBounds = false
thumbView.isUserInteractionEnabled = false
self.addSubview(thumbView)
// thumb image
self.thumbImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: thumbView.frame.size.width, height: thumbView.frame.size.height))
thumbImageView.contentMode = UIViewContentMode.center
thumbImageView.autoresizingMask = UIViewAutoresizing.flexibleWidth
thumbView.addSubview(thumbImageView)
self.on = false
}
override open func beginTracking(_ touch: UITouch, with event: UIEvent?) -> Bool {
super.beginTracking(touch, with: event)
startTrackingValue = self.on
didChangeWhileTracking = false
let activeKnobWidth = self.bounds.size.height - 2 + 5
isAnimating = true
UIView.animate(withDuration: 0.3, delay: 0.0, options: [UIViewAnimationOptions.curveEaseOut, UIViewAnimationOptions.beginFromCurrentState], animations: {
if self.on {
self.thumbView.frame = CGRect(x: self.bounds.size.width - (activeKnobWidth + 1), y: self.thumbView.frame.origin.y, width: activeKnobWidth, height: self.thumbView.frame.size.height)
self.backgroundView.backgroundColor = self.onTintColor
self.thumbView.backgroundColor = self.onThumbTintColor
}
else {
self.thumbView.frame = CGRect(x: self.thumbView.frame.origin.x, y: self.thumbView.frame.origin.y, width: activeKnobWidth, height: self.thumbView.frame.size.height)
self.backgroundView.backgroundColor = self.activeColor
self.thumbView.backgroundColor = self.thumbTintColor
}
}, completion: { finished in
self.isAnimating = false
})
let shadowAnim = CABasicAnimation(keyPath: "shadowPath")
shadowAnim.duration = 0.3
shadowAnim.fromValue = thumbView.layer.shadowPath
shadowAnim.toValue = UIBezierPath(roundedRect: thumbView.bounds, cornerRadius: thumbView.layer.cornerRadius).cgPath
thumbView.layer.add(shadowAnim, forKey: "shadowPath")
thumbView.layer.shadowPath = UIBezierPath(roundedRect: thumbView.bounds, cornerRadius: thumbView.layer.cornerRadius).cgPath
return true
}
override open func continueTracking(_ touch: UITouch, with event: UIEvent?) -> Bool {
super.continueTracking(touch, with: event)
// Get touch location
let lastPoint = touch.location(in: 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(true)
if !startTrackingValue {
didChangeWhileTracking = true
}
}
else {
self.showOff(true)
if startTrackingValue {
didChangeWhileTracking = true
}
}
return true
}
override open func endTracking(_ touch: UITouch?, with event: UIEvent?) {
super.endTracking(touch, with: event)
let previousValue = self.on
if didChangeWhileTracking {
self.setOn(currentVisualValue, animated: true)
}
else {
self.setOn(!self.on, animated: true)
}
if previousValue != self.on {
self.sendActions(for: UIControlEvents.valueChanged)
}
}
override open func cancelTracking(with event: UIEvent?) {
super.cancelTracking(with: event)
// just animate back to the original value
if self.on {
self.showOn(true)
}
else {
self.showOff(true)
}
}
override open func layoutSubviews() {
super.layoutSubviews()
if !isAnimating {
let frame = self.frame
// background
backgroundView.frame = CGRect(x: 0, y: 0, width: frame.size.width, height: frame.size.height)
backgroundView.layer.cornerRadius = self.isRounded ? frame.size.height * 0.5 : 2
// images
onImageView.frame = CGRect(x: 0, y: 0, width: frame.size.width - frame.size.height, height: frame.size.height)
offImageView.frame = CGRect(x: frame.size.height, y: 0, width: frame.size.width - frame.size.height, height: frame.size.height)
self.onLabel.frame = CGRect(x: 0, y: 0, width: frame.size.width - frame.size.height, height: frame.size.height)
self.offLabel.frame = CGRect(x: frame.size.height, y: 0, width: frame.size.width - frame.size.height, height: frame.size.height)
// thumb
let normalKnobWidth = frame.size.height - 2
if self.on {
thumbView.frame = CGRect(x: frame.size.width - (normalKnobWidth + 1), y: 1, width: frame.size.height - 2, height: normalKnobWidth)
thumbImageView.frame = CGRect(x: frame.size.width - normalKnobWidth, y: 0, width: normalKnobWidth, height: normalKnobWidth)
}
else {
thumbView.frame = CGRect(x: 1, y: 1, width: normalKnobWidth, height: normalKnobWidth)
thumbImageView.frame = CGRect(x: 0, y: 0, width: normalKnobWidth, height: normalKnobWidth)
}
thumbView.layer.cornerRadius = self.isRounded ? (frame.size.height * 0.5) - 1 : 2
thumbView.layer.shadowPath = UIBezierPath(roundedRect: thumbView.bounds, cornerRadius: thumbView.layer.cornerRadius).cgPath
}
}
/*
* Set the state of the switch to on or off, optionally animating the transition.
*/
open func setOn(_ isOn: Bool, animated: Bool) {
switchValue = isOn
if on {
self.showOn(animated)
}
else {
self.showOff(animated)
}
}
/*
* Detects whether the switch is on or off
*
* @return BOOL YES if switch is on. NO if switch is off
*/
open func isOn() -> Bool {
return self.on
}
/*
* update the looks of the switch to be in the on position
* optionally make it animated
*/
fileprivate func showOn(_ animated: Bool) {
let normalKnobWidth = self.bounds.size.height - 2
let activeKnobWidth = normalKnobWidth + 5
if animated {
isAnimating = true
UIView.animate(withDuration: 0.3, delay: 0.0, options: [UIViewAnimationOptions.curveEaseOut, UIViewAnimationOptions.beginFromCurrentState], animations: {
if self.isTracking {
self.thumbView.frame = CGRect(x: self.bounds.size.width - (activeKnobWidth + 1), y: self.thumbView.frame.origin.y, width: activeKnobWidth, height: self.thumbView.frame.size.height)
}
else {
self.thumbView.frame = CGRect(x: self.bounds.size.width - (normalKnobWidth + 1), y: self.thumbView.frame.origin.y, width: normalKnobWidth, height: self.thumbView.frame.size.height)
}
self.backgroundView.backgroundColor = self.onTintColor
self.backgroundView.layer.borderColor = self.onTintColor.cgColor
self.thumbView.backgroundColor = self.onThumbTintColor
self.onImageView.alpha = 1.0
self.offImageView.alpha = 0
self.onLabel.alpha = 1.0
self.offLabel.alpha = 0
}, completion: { finished in
self.isAnimating = false
})
let shadowAnim = CABasicAnimation(keyPath: "shadowPath")
shadowAnim.duration = 0.3
shadowAnim.fromValue = thumbView.layer.shadowPath
shadowAnim.toValue = UIBezierPath(roundedRect: thumbView.bounds, cornerRadius: thumbView.layer.cornerRadius).cgPath
thumbView.layer.add(shadowAnim, forKey: "shadowPath")
thumbView.layer.shadowPath = UIBezierPath(roundedRect: thumbView.bounds, cornerRadius: thumbView.layer.cornerRadius).cgPath
}
else {
if self.isTracking {
thumbView.frame = CGRect(x: self.bounds.size.width - (activeKnobWidth + 1), y: thumbView.frame.origin.y, width: activeKnobWidth, height: thumbView.frame.size.height)
}
else {
thumbView.frame = CGRect(x: self.bounds.size.width - (normalKnobWidth + 1), y: thumbView.frame.origin.y, width: normalKnobWidth, height: thumbView.frame.size.height)
}
backgroundView.backgroundColor = self.onTintColor
backgroundView.layer.borderColor = self.onTintColor.cgColor
thumbView.backgroundColor = self.onThumbTintColor
onImageView.alpha = 1.0
offImageView.alpha = 0
onLabel.alpha = 1.0
offLabel.alpha = 0
}
currentVisualValue = true
}
/*
* update the looks of the switch to be in the off position
* optionally make it animated
*/
fileprivate func showOff(_ animated: Bool) {
let normalKnobWidth = self.bounds.size.height - 2
let activeKnobWidth = normalKnobWidth + 5
if animated {
isAnimating = true
UIView.animate(withDuration: 0.3, delay: 0.0, options: [UIViewAnimationOptions.curveEaseOut, UIViewAnimationOptions.beginFromCurrentState], animations: {
if self.isTracking {
self.thumbView.frame = CGRect(x: 1, y: self.thumbView.frame.origin.y, width: activeKnobWidth, height: self.thumbView.frame.size.height);
self.backgroundView.backgroundColor = self.activeColor
}
else {
self.thumbView.frame = CGRect(x: 1, y: self.thumbView.frame.origin.y, width: normalKnobWidth, height: self.thumbView.frame.size.height);
self.backgroundView.backgroundColor = self.inactiveColor
}
self.backgroundView.layer.borderColor = self.borderColor.cgColor
self.thumbView.backgroundColor = self.thumbTintColor
self.onImageView.alpha = 0
self.offImageView.alpha = 1.0
self.onLabel.alpha = 0
self.offLabel.alpha = 1.0
}, completion: { finished in
self.isAnimating = false
})
let shadowAnim = CABasicAnimation(keyPath: "shadowPath")
shadowAnim.duration = 0.3
shadowAnim.fromValue = thumbView.layer.shadowPath
shadowAnim.toValue = UIBezierPath(roundedRect: thumbView.bounds, cornerRadius: thumbView.layer.cornerRadius).cgPath
thumbView.layer.add(shadowAnim, forKey: "shadowPath")
thumbView.layer.shadowPath = UIBezierPath(roundedRect: thumbView.bounds, cornerRadius: thumbView.layer.cornerRadius).cgPath
}
else {
if (self.isTracking) {
thumbView.frame = CGRect(x: 1, y: thumbView.frame.origin.y, width: activeKnobWidth, height: thumbView.frame.size.height)
backgroundView.backgroundColor = self.activeColor
}
else {
thumbView.frame = CGRect(x: 1, y: thumbView.frame.origin.y, width: normalKnobWidth, height: thumbView.frame.size.height)
backgroundView.backgroundColor = self.inactiveColor
}
backgroundView.layer.borderColor = self.borderColor.cgColor
thumbView.backgroundColor = self.thumbTintColor
onImageView.alpha = 0
offImageView.alpha = 1.0
onLabel.alpha = 0
offLabel.alpha = 1.0
}
currentVisualValue = false
}
}