-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathButton.swift
415 lines (346 loc) · 11.1 KB
/
Button.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
//
// Button.swift
//
// Made with ❤️ by Novum
//
// Copyright © Telefonica. All rights reserved.
//
import UIKit
open class Button: UIControl {
private enum Constants {
static let animationDuration: TimeInterval = 0.3
static let animationCurveControlPoint1 = CGPoint(x: 0.77, y: 0)
static let animationCurveControlPoint2 = CGPoint(x: 0.175, y: 1)
static let borderWidth: CGFloat = 1.5
static let enabledAlpha: CGFloat = 1.0
static let disabledAlpha: CGFloat = 0.5
}
public struct Style {
public let allowsBleedingAlignment: Bool
public let stateStyleByState: [State: StateStyle]
public var overriddenSizes: OverriddenSizes?
public struct OverriddenSizes {
public let insets: UIEdgeInsets
public let minimumWidth: CGFloat
public let font: UIFont
public let leftImageHeight: CGFloat?
public let rightImageHeight: CGFloat?
public init(
insets: UIEdgeInsets,
minimumWidth: CGFloat,
font: UIFont,
leftImageHeight: CGFloat? = nil,
rightImageHeight: CGFloat? = nil
) {
self.insets = insets
self.minimumWidth = minimumWidth
self.font = font
self.leftImageHeight = leftImageHeight
self.rightImageHeight = rightImageHeight
}
}
public init(allowsBleedingAlignment: Bool,
stateStyleByState: [State: Button.StateStyle],
overriddenSizes: Button.Style.OverriddenSizes? = nil) {
self.allowsBleedingAlignment = allowsBleedingAlignment
self.stateStyleByState = stateStyleByState
self.overriddenSizes = overriddenSizes
}
}
public struct StateStyle {
public let textColor: UIColor
public let backgroundColor: UIColor
public let borderColor: UIColor
public init(textColor: UIColor, backgroundColor: UIColor, borderColor: UIColor) {
self.textColor = textColor
self.backgroundColor = backgroundColor
self.borderColor = borderColor
}
}
public enum LeftImage {
case custom(image: UIImage)
var image: UIImage {
switch self {
case .custom(let image): return image.withRenderingMode(.alwaysTemplate)
}
}
var space: CGFloat {
switch self {
case .custom: return 8
}
}
}
public enum RightImage {
case chevron
case custom(image: UIImage)
private var _image: UIImage {
switch self {
case .chevron: return .chevron
case let .custom(image: image): return image
}
}
var image: UIImage {
_image.withRenderingMode(.alwaysTemplate)
}
var space: CGFloat {
switch self {
case .chevron: return 2
case .custom: return 8
}
}
}
public var style: Style {
didSet {
updateStyle()
}
}
public var isSmall: Bool {
didSet {
updateStyle()
}
}
@objc public var title: String? {
get { container.title }
set { container.title = newValue }
}
@objc public var loadingTitle: String? {
get { container.loadingTitle }
set { container.loadingTitle = newValue }
}
public var leftImage: LeftImage? {
get { container.leftImage }
set { container.leftImage = newValue }
}
public var rightImage: RightImage? {
get { container.rightImage }
set { container.rightImage = newValue }
}
private var overridenAccessibilityLabel: String?
private var isShowingLoadingAnimation = false
private lazy var animator = UIViewPropertyAnimator(
duration: Constants.animationDuration,
controlPoint1: Constants.animationCurveControlPoint1,
controlPoint2: Constants.animationCurveControlPoint2
)
private lazy var container = ButtonContentView()
public convenience init() {
self.init(title: "")
}
public init(
style: Style = .primary,
title: String,
leftImage: LeftImage? = nil,
rightImage: RightImage? = nil,
loadingTitle: String? = nil,
isSmall: Bool = false
) {
self.style = style
self.isSmall = isSmall
super.init(frame: .zero)
self.title = title
self.leftImage = leftImage
self.rightImage = rightImage
self.loadingTitle = loadingTitle
commonInit()
}
public required init?(coder: NSCoder) {
style = .primary
isSmall = false
super.init(coder: coder)
commonInit()
}
override public var contentMode: UIView.ContentMode {
get { super.contentMode }
set {
super.contentMode = newValue
updateInsets()
}
}
override public var alignmentRectInsets: UIEdgeInsets {
UIEdgeInsets(top: 0, left: leftBleedingInsets, bottom: 0, right: rightBleedingInsets)
}
override public var intrinsicContentSize: CGSize {
container.intrinsicContentSize
}
public var isLoading = false {
didSet {
didUpdateState()
}
}
override open var isHighlighted: Bool {
didSet {
didUpdateState()
}
}
override open var isSelected: Bool {
didSet {
didUpdateState()
}
}
override open var isEnabled: Bool {
didSet {
didUpdateState()
}
}
override open var state: UIControl.State {
if isLoading {
return .loading
} else if !isEnabled {
return .disabled
} else if isSelected || isHighlighted {
return .selected
} else {
return .normal
}
}
override open func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
guard !isLoading else { return nil }
return super.hitTest(point, with: event)
}
override open func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
guard traitCollection.userInterfaceStyle != previousTraitCollection?.userInterfaceStyle else { return }
applyStyleColors()
}
override open func layoutSublayers(of layer: CALayer) {
super.layoutSublayers(of: layer)
setMisticaRadius(.button)
}
}
@objc public extension Button {
override var accessibilityLabel: String? {
get {
if isLoading {
return loadingTitle
} else if overridenAccessibilityLabel != nil {
return overridenAccessibilityLabel
} else {
return title
}
}
set {
overridenAccessibilityLabel = newValue
}
}
}
private extension Button {
var styleInsets: UIEdgeInsets {
style.insets(isSmall: isSmall)
}
var leftBleedingInsets: CGFloat {
guard style.allowsBleedingAlignment else { return 0 }
guard contentMode == .left else { return 0 }
return styleInsets.left
}
var rightBleedingInsets: CGFloat {
guard style.allowsBleedingAlignment else { return 0 }
guard contentMode == .right else { return 0 }
return styleInsets.right
}
func updateInsets() {
var insets = styleInsets
insets.left -= leftBleedingInsets
insets.right -= rightBleedingInsets
container.layoutMargins = insets
container.insetsLayoutMarginsFromSafeArea = false
invalidateIntrinsicContentSize()
}
func updateStyle() {
applyStyleColors()
updateInsets()
container.minimumWidth = style.minimumWidth(isSmall: isSmall)
container.font = style.font(isSmall: isSmall)
container.leftImageHeight = style.leftImageHeight(isSmall: isSmall)
container.rightImageHeight = style.rightImageHeight(isSmall: isSmall)
}
func commonInit() {
setUpView()
setUpContainer()
updateStyle()
}
func setUpView() {
layer.borderWidth = Constants.borderWidth
layer.masksToBounds = true
isAccessibilityElement = true
updateTraits()
}
func setUpContainer() {
container.isUserInteractionEnabled = false
addSubview(withDefaultConstraints: container)
}
func applyStyleColors() {
guard let stateStyle = style.stateStyleByState[state] else {
preconditionFailure("Style \(style) does not have stateStyle for state \(state). Check that the current style is defined properly.")
}
container.textColor = stateStyle.textColor
backgroundColor = stateStyle.backgroundColor
layer.borderColor = stateStyle.borderColor.cgColor
alpha = state != .disabled ? Constants.enabledAlpha : Constants.disabledAlpha
}
func didUpdateState() {
if isLoading {
animator.stopAnimation(true)
// transition to loading
UIAccessibility.post(notification: .layoutChanged, argument: loadingTitle)
container.willTransitionToLoading()
animator.addAnimations { [weak self] in
self?.container.transitionToLoading()
self?.applyStyleColors()
}
updateTraits()
isShowingLoadingAnimation = true
animator.startAnimation()
} else if isShowingLoadingAnimation {
animator.stopAnimation(true)
// transition to normal
animator.addAnimations { [weak self] in
self?.container.transitionToNormal()
self?.applyStyleColors()
}
animator.addCompletion { [weak self] _ in
guard let s = self else { return }
UIAccessibility.post(notification: .layoutChanged, argument: s.accessibilityLabel)
s.updateTraits()
}
isShowingLoadingAnimation = false
animator.startAnimation()
} else {
applyStyleColors()
updateTraits()
}
}
func updateTraits() {
accessibilityTraits = state.accesibilityTraits
}
}
private extension Button.State {
var accesibilityTraits: UIAccessibilityTraits {
if contains(.disabled) || contains(.loading) {
return [.button, .notEnabled]
} else {
return .button
}
}
}
// MARK: Objective-C API
@objc public extension Button {
func objc_setNormalState() {
isLoading = false
isSelected = false
isEnabled = true
}
func objc_setLoadingState() {
isLoading = true
}
func objc_setDisabledState() {
isEnabled = false
}
func objc_setLinkStyle() {
style = .link
}
}
// MARK: Useful extensions
public extension Button.State {
static let loading = UIControl.State(rawValue: 1 << 50) // Arbitrary value
}
extension Button.State: Swift.Hashable {}