-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathSwitch.swift
96 lines (73 loc) · 3.11 KB
/
Switch.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
// Note: we deliberately don't wrap UISwitch.
// This allows us to have a somewhat custom API free of objc selectors etc.
open class Switch: UIControl {
private let tapGestureRecognizer: UITapGestureRecognizer
private var thumb = SwitchThumb()
public var onTintColor: UIColor? = defaultOnTintColor
public var thumbTintColor: UIColor? = defaultThumbColor
public var onPress: ((Bool) -> Void)?
public var isOn = false {
didSet {
thumb.frame.origin.x = isOn ? frame.width - thumb.frame.width : 0
thumb.backgroundColor = isOn ? thumbTintColor : offThumbColor
backgroundColor = isOn ? onTintColor : offTrackColor
}
}
public init() {
tapGestureRecognizer = UITapGestureRecognizer(onPress: nil)
super.init(
frame: CGRect(origin: CGPoint(x: 0, y: 0), size: frameSize)
)
layer.cornerRadius = frame.height / 2
backgroundColor = isOn ? onTintColor : .lightGray
thumb.frame.midY = frame.midY
thumb.backgroundColor = isOn ? thumbTintColor : .white
addSubview(thumb)
tapGestureRecognizer.view = self
tapGestureRecognizer.onPress = {
self.setOn(!self.isOn, animated: true)
self.onPress?(self.isOn)
}
addGestureRecognizer(tapGestureRecognizer)
}
public func setOn(_ isOn: Bool, animated: Bool) {
if animated {
UIView.animate(withDuration: animationTime) {
self.isOn = isOn
}
} else {
self.isOn = isOn
}
}
}
@MainActor
private final class SwitchThumb: UIView {
let thumb = UIView(frame: .zero)
let shadow = UIView(frame: .zero)
override var backgroundColor: UIColor? {
get { thumb.backgroundColor }
set { thumb.backgroundColor = newValue }
}
init() {
thumb.frame.size = CGSize(width: thumbSize, height: thumbSize)
thumb.layer.cornerRadius = thumbSize / 2
let shadowSize = thumbSize + 2
shadow.frame.size = CGSize(width: shadowSize, height: shadowSize)
shadow.frame.midX = thumb.frame.midX
shadow.frame.midY = thumb.frame.midY + 2
shadow.layer.cornerRadius = shadowSize / 2
shadow.backgroundColor = .lightGray.withAlphaComponent(0.4)
super.init(frame: .zero)
frame.size = CGSize(width: thumbSize, height: thumbSize)
layer.cornerRadius = thumbSize / 2
self.addSubview(shadow)
self.addSubview(thumb)
}
}
@MainActor private let frameSize = CGSize(width: 51, height: 21)
@MainActor private let thumbSize = CGFloat(31.0)
@MainActor private let animationTime = 0.25
@MainActor private let defaultOnTintColor = CGColor(red: 169 / 255, green: 218 / 255 , blue: 214 / 255, alpha: 1)
@MainActor private let defaultThumbColor = CGColor(red: 14 / 255, green: 136 / 255 , blue: 122 / 255, alpha: 1)
@MainActor private let offThumbColor = CGColor(red: 176 / 255, green: 176 / 255 , blue: 176 / 255, alpha: 1)
@MainActor private let offTrackColor = CGColor(red: 225 / 255, green: 225 / 255 , blue: 225 / 255, alpha: 1)