-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathRadioButton.swift
159 lines (126 loc) · 4.7 KB
/
RadioButton.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
//
// RadioButton.swift
//
// Made with ❤️ by Novum
//
// Copyright © Telefonica. All rights reserved.
//
import CoreGraphics
import UIKit
public class RadioButton: UIControl {
private enum Constants {
static let viewWidth = CGFloat(20)
static let animationDuration = Double(0.4)
static let timingFunction = CAMediaTimingFunction(controlPoints: 0.77, 0, 0.175, 1)
}
private var _isActivated = false
/// A Boolean value that determines the off/on state of the RadioButton
public var isActivated: Bool {
get {
_isActivated
}
set {
guard _isActivated != newValue else { return }
_isActivated = newValue
updateViewStyleAnimated(activated: newValue)
}
}
// Called when the RadioButton changes its value.
public var onValueChanged: ((Bool) -> Void)?
public convenience init() {
self.init(frame: .zero)
}
override public init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
}
override public func layoutSubviews() {
super.layoutSubviews()
layer.cornerRadius = bounds.width / 2.0
layer.masksToBounds = true
}
override public var accessibilityTraits: UIAccessibilityTraits {
get {
if isActivated {
return super.accessibilityTraits.union([.button, .selected])
} else {
return super.accessibilityTraits.union([.button])
}
}
set {
super.accessibilityTraits = newValue
}
}
override public var allControlEvents: UIControl.Event {
[.valueChanged]
}
override public var intrinsicContentSize: CGSize {
let diameter = UIFontMetrics.default.scaledValue(for: Constants.viewWidth)
return CGSize(width: diameter, height: diameter)
}
override public func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
guard traitCollection.userInterfaceStyle != previousTraitCollection?.userInterfaceStyle else { return }
updateViewStyleAnimated(activated: isActivated)
}
public func setActivated(_ activated: Bool, animated: Bool) {
guard _isActivated != activated else { return }
_isActivated = activated
if animated {
updateViewStyleAnimated(activated: activated)
} else {
updateViewStyle(activated: activated)
}
}
}
private extension RadioButton {
func commonInit() {
updateViewStyle(activated: isActivated)
setContentHuggingPriority(.required, for: .horizontal)
setContentHuggingPriority(.required, for: .vertical)
setContentCompressionResistancePriority(.required, for: .horizontal)
setContentCompressionResistancePriority(.required, for: .vertical)
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(didTap))
addGestureRecognizer(tapGesture)
isAccessibilityElement = true
}
@objc func didTap() {
isActivated.toggle()
sendActions(for: .valueChanged)
onValueChanged?(isActivated)
let generator = UIImpactFeedbackGenerator(style: .light)
generator.impactOccurred()
}
func updateViewStyleAnimated(activated: Bool) {
let animation = CAAnimationGroup()
let duration = Constants.animationDuration
let borderWidthAnimation = CABasicAnimation(keyPath: "borderWidth")
borderWidthAnimation.fromValue = layer.borderWidth
borderWidthAnimation.duration = duration
let borderColorAnimation = CABasicAnimation(keyPath: "borderColor")
borderColorAnimation.fromValue = layer.borderColor
borderColorAnimation.duration = duration
updateViewStyle(activated: activated)
borderWidthAnimation.toValue = layer.borderWidth
borderColorAnimation.toValue = layer.borderColor
animation.animations = [borderWidthAnimation, borderColorAnimation]
animation.duration = duration
animation.timingFunction = Constants.timingFunction
layer.add(animation, forKey: "group")
}
func updateViewStyle(activated: Bool) {
if activated {
layer.backgroundColor = UIColor.white.cgColor
layer.borderColor = UIColor.controlActivated.cgColor
layer.borderWidth = intrinsicContentSize.width / 4.0
} else {
layer.backgroundColor = UIColor.background.cgColor
layer.borderColor = UIColor.control.cgColor
layer.borderWidth = 1
}
}
}