-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSTProgressHUD.swift
More file actions
777 lines (681 loc) · 28.9 KB
/
Copy pathSTProgressHUD.swift
File metadata and controls
777 lines (681 loc) · 28.9 KB
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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
//
// STHUD.swift
// STBaseProject
//
// Created by 寒江孤影 on 2017/10/14.
//
import CoreGraphics
import Foundation
import UIKit
public protocol STProgressHUDDelegate: AnyObject {
func hudWasHidden(_ hud: STProgressHUD)
}
extension STProgressHUDDelegate {
func hudWasHidden(_ hud: STProgressHUD) {}
}
/// HUD animation behavior used by show/hide APIs.
public enum STHUDAnimation: Equatable {
case none
case fade
case zoom
case zoomIn
case zoomOut
public var isAnimated: Bool { self != .none }
}
public class STProgressHUD: UIView {
public enum HudMode {
case indeterminate, determinate, determinateHorizontalBar, annularDeterminate, customView, text
}
public enum HudAnimation {
case fade, zoom, zoomOut, zoomIn
}
public static let maxOffset: CGFloat = 1_000_000.0
public var progress: Float = 0.0 {
didSet {
guard oldValue != self.progress else { return }
(self.indicator as? STProgressView)?.progress = self.progress
}
}
public var progressObject: Progress? {
didSet {
guard oldValue !== self.progressObject else { return }
self.setProgressDisplayLinkEnabled(true)
}
}
public var customView: UIView? {
didSet {
guard oldValue != self.customView, self.mode == .customView else { return }
self.updateIndicators()
}
}
public var mode: HudMode = .indeterminate {
didSet {
guard self.mode != oldValue else { return }
self.updateIndicators()
}
}
public var contentColor = UIColor(white: 0, alpha: 0.7) {
didSet {
guard oldValue != self.contentColor else { return }
self.updateViews(forColor: self.contentColor)
}
}
public var animationType: HudAnimation = .fade
public var offset: CGPoint = .zero
public var margin: CGFloat = 20.0
public var minSize: CGSize = .zero
public var isSquare = false
public var isDefaultMotionEffectsEnabled = true
public var minShowTime: TimeInterval = 0.0
public var graceTime: TimeInterval = 0.0
public var removeFromSuperViewOnHide: Bool = false
public var completionBlock: (() -> Void)?
public weak var delegate: STProgressHUDDelegate?
public private(set) lazy var backgroundView: STProgressHUDBackgroundView = {
let view = STProgressHUDBackgroundView(frame: self.bounds)
view.style = .solidColor
view.backgroundColor = .clear
view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
view.alpha = 0
return view
}()
public private(set) lazy var bezelView: STProgressHUDBackgroundView = {
let view = STProgressHUDBackgroundView()
view.translatesAutoresizingMaskIntoConstraints = false
view.layer.cornerRadius = 5
view.alpha = 0
return view
}()
public private(set) lazy var label: UILabel = {
let lbl = UILabel()
lbl.adjustsFontSizeToFitWidth = false
lbl.textAlignment = .center
lbl.textColor = self.contentColor
lbl.font = UIFont.st_boldSystemFont(ofSize: self.defaultLabelFontSize)
lbl.isOpaque = false
lbl.backgroundColor = .clear
lbl.translatesAutoresizingMaskIntoConstraints = false
lbl.setContentCompressionResistancePriority(.init(rawValue: 998), for: .horizontal)
lbl.setContentCompressionResistancePriority(.init(rawValue: 998), for: .vertical)
return lbl
}()
public private(set) lazy var detailsLabel: UILabel = {
let lbl = UILabel()
lbl.adjustsFontSizeToFitWidth = false
lbl.textAlignment = .center
lbl.textColor = self.contentColor
lbl.font = UIFont.st_boldSystemFont(ofSize: self.defaultDetailsLabelFontSize)
lbl.isOpaque = false
lbl.backgroundColor = .clear
lbl.translatesAutoresizingMaskIntoConstraints = false
lbl.setContentCompressionResistancePriority(.init(rawValue: 998), for: .horizontal)
lbl.setContentCompressionResistancePriority(.init(rawValue: 998), for: .vertical)
return lbl
}()
public private(set) lazy var button: UIButton = {
let btn = STProgressHUDRoundedButton()
btn.titleLabel?.textAlignment = .center
btn.titleLabel?.font = UIFont.st_boldSystemFont(ofSize: self.defaultDetailsLabelFontSize)
btn.setTitleColor(self.contentColor, for: .normal)
btn.translatesAutoresizingMaskIntoConstraints = false
btn.setContentCompressionResistancePriority(.init(rawValue: 998), for: .horizontal)
btn.setContentCompressionResistancePriority(.init(rawValue: 998), for: .vertical)
return btn
}()
override init(frame: CGRect) {
super.init(frame: frame)
self.commonInit()
}
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.commonInit()
}
convenience init(withView view: UIView) {
self.init(frame: view.bounds)
}
deinit {
self.unregisterFromNotifications()
}
@discardableResult
public class func show(addedToView view: UIView, animation: STHUDAnimation = .fade) -> STProgressHUD {
let hud = STProgressHUD(withView: view)
hud.removeFromSuperViewOnHide = true
view.addSubview(hud)
hud.show(animation: animation)
return hud
}
@available(*, deprecated, renamed: "show(addedToView:animation:)")
@discardableResult
public class func show(addedToView view: UIView, animated: Bool) -> STProgressHUD {
return show(addedToView: view, animation: animated ? .fade : .none)
}
@discardableResult
public class func hide(addedToView view: UIView, animation: STHUDAnimation = .fade) -> Bool {
guard let hud = hudForView(view) else { return false }
hud.removeFromSuperViewOnHide = true
hud.hide(animation: animation)
return true
}
@available(*, deprecated, renamed: "hide(addedToView:animation:)")
@discardableResult
public class func hide(addedToView view: UIView, animated: Bool) -> Bool {
guard let hud = hudForView(view) else { return false }
hud.removeFromSuperViewOnHide = true
hud.hide(animated: animated)
return true
}
public class func hudForView(_ view: UIView) -> STProgressHUD? {
return view.subviews.reversed().first { $0 is STProgressHUD } as? STProgressHUD
}
public func show(animation: STHUDAnimation = .fade) {
self.animationType = animation.hudAnimation
self.showCore(animated: animation.isAnimated)
}
@available(*, deprecated, renamed: "show(animation:)")
public func show(animated: Bool) {
self.showCore(animated: animated)
}
public func hide(animation: STHUDAnimation = .fade) {
self.animationType = animation.hudAnimation
self.hideCore(animated: animation.isAnimated)
}
@available(*, deprecated, renamed: "hide(animation:)")
public func hide(animated: Bool) {
self.hideCore(animated: animated)
}
public func hide(animation: STHUDAnimation = .fade, afterDelay delay: TimeInterval) {
self.hideCore(animation: animation, afterDelay: delay)
}
@available(*, deprecated, renamed: "hide(animation:afterDelay:)")
public func hide(animated: Bool, afterDelay delay: TimeInterval) {
self.hideCore(animation: animated ? self.animationType.stHUDAnimation : .none, afterDelay: delay)
}
private func showCore(animated: Bool) {
assert(Thread.isMainThread, "STProgressHUD needs to be accessed on the main thread.")
self.minShowTimer?.invalidate()
self.isUseAnimation = animated
self.isFinished = false
if self.graceTime > 0.0 {
let timer = Timer(
timeInterval: self.graceTime,
target: self,
selector: #selector(handleGraceTimer(_:)),
userInfo: nil,
repeats: false
)
RunLoop.current.add(timer, forMode: .common)
self.graceTimer = timer
} else {
self.showUsingAnimation(animated)
}
}
private func hideCore(animated: Bool) {
assert(Thread.isMainThread, "STProgressHUD needs to be accessed on the main thread.")
self.graceTimer?.invalidate()
self.isUseAnimation = animated
self.isFinished = true
if self.minShowTime > 0.0, let showStarted = self.showStarted {
let interval = Date().timeIntervalSince(showStarted)
if interval < self.minShowTime {
let timer = Timer(
timeInterval: self.minShowTime - interval,
target: self,
selector: #selector(handleMinShowTimer(_:)),
userInfo: nil,
repeats: false
)
RunLoop.current.add(timer, forMode: .common)
self.minShowTimer = timer
return
}
}
self.hideUsingAnimation(self.isUseAnimation)
}
private func hideCore(animation: STHUDAnimation, afterDelay delay: TimeInterval) {
let timer = Timer(
timeInterval: delay,
target: self,
selector: #selector(handleHideTimer(_:)),
userInfo: animation,
repeats: false
)
RunLoop.current.add(timer, forMode: .common)
self.hideDelayTimer = timer
}
override public func updateConstraints() {
let metrics: [String: Any] = ["margin": self.margin]
var subviews: [UIView] = [self.topSpacer, self.label, self.detailsLabel, self.button, self.bottomSpacer]
if let indicator = self.indicator {
subviews.insert(indicator, at: 1)
}
self.removeConstraints(self.constraints)
self.topSpacer.removeConstraints(self.topSpacer.constraints)
self.bottomSpacer.removeConstraints(self.bottomSpacer.constraints)
self.bezelView.removeConstraints(self.bezelConstraints)
self.bezelConstraints = []
let centeringConstraints: [NSLayoutConstraint] = [
NSLayoutConstraint(item: self.bezelView, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: self.offset.x),
NSLayoutConstraint(item: self.bezelView, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: self.offset.y)
]
self.apply(priority: .init(rawValue: 998), toConstraints: centeringConstraints)
self.addConstraints(centeringConstraints)
var sideConstraints: [NSLayoutConstraint] = []
sideConstraints += NSLayoutConstraint.constraints(withVisualFormat: "|-(>=margin)-[bezel]-(>=margin)-|", options: .alignAllTop, metrics: metrics, views: ["bezel": self.bezelView])
sideConstraints += NSLayoutConstraint.constraints(withVisualFormat: "V:|-(>=margin)-[bezel]-(>=margin)-|", options: .alignAllTop, metrics: metrics, views: ["bezel": self.bezelView])
self.apply(priority: .init(rawValue: 999), toConstraints: sideConstraints)
self.addConstraints(sideConstraints)
if self.minSize != .zero {
let miniSizeConstraints: [NSLayoutConstraint] = [
NSLayoutConstraint(item: self.bezelView, attribute: .width, relatedBy: .greaterThanOrEqual, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: self.minSize.width),
NSLayoutConstraint(item: self.bezelView, attribute: .height, relatedBy: .greaterThanOrEqual, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: self.minSize.height)
]
self.apply(priority: .init(rawValue: 997), toConstraints: miniSizeConstraints)
self.bezelConstraints += miniSizeConstraints
}
if self.isSquare {
let square = NSLayoutConstraint(item: self.bezelView, attribute: .height, relatedBy: .equal, toItem: self.bezelView, attribute: .width, multiplier: 1, constant: 0)
square.priority = .init(rawValue: 997)
self.bezelConstraints.append(square)
}
self.topSpacer.addConstraint(NSLayoutConstraint(item: self.topSpacer, attribute: .height, relatedBy: .greaterThanOrEqual, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: self.margin))
self.bottomSpacer.addConstraint(NSLayoutConstraint(item: self.bottomSpacer, attribute: .height, relatedBy: .greaterThanOrEqual, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: self.margin))
self.bezelConstraints.append(NSLayoutConstraint(item: self.topSpacer, attribute: .height, relatedBy: .equal, toItem: self.bottomSpacer, attribute: .height, multiplier: 1, constant: 0))
self.paddingConstraints = []
for (index, view) in subviews.enumerated() {
self.bezelConstraints.append(NSLayoutConstraint(item: view, attribute: .centerX, relatedBy: .equal, toItem: self.bezelView, attribute: .centerX, multiplier: 1, constant: 0))
self.bezelConstraints += NSLayoutConstraint.constraints(withVisualFormat: "|-(>=margin)-[view]-(>=margin)-|", options: .alignAllTop, metrics: metrics, views: ["view": view])
if index == 0 {
self.bezelConstraints.append(NSLayoutConstraint(item: view, attribute: .top, relatedBy: .equal, toItem: self.bezelView, attribute: .top, multiplier: 1, constant: 0))
} else if index == subviews.count - 1 {
self.bezelConstraints.append(NSLayoutConstraint(item: view, attribute: .bottom, relatedBy: .equal, toItem: self.bezelView, attribute: .bottom, multiplier: 1, constant: 0))
}
if index > 0 {
let padding = NSLayoutConstraint(item: view, attribute: .top, relatedBy: .equal, toItem: subviews[index - 1], attribute: .bottom, multiplier: 1, constant: 0)
self.bezelConstraints.append(padding)
self.paddingConstraints.append(padding)
}
}
self.bezelView.addConstraints(self.bezelConstraints)
self.updatePaddingConstraints()
super.updateConstraints()
}
override public func layoutSubviews() {
if !self.needsUpdateConstraints() {
self.updatePaddingConstraints()
}
super.layoutSubviews()
}
private let defaultPadding: CGFloat = 4.0
private let defaultLabelFontSize: CGFloat = 16.0
private let defaultDetailsLabelFontSize: CGFloat = 12.0
private var isUseAnimation: Bool = false
private var isFinished: Bool = true
private var showStarted: Date?
private var indicator: UIView?
private var paddingConstraints: [NSLayoutConstraint] = []
private var bezelConstraints: [NSLayoutConstraint] = []
private var graceTimer: Timer?
private var minShowTimer: Timer?
private var hideDelayTimer: Timer?
private var progressObjectDisplayLink: CADisplayLink? {
willSet {
guard newValue !== self.progressObjectDisplayLink else { return }
self.progressObjectDisplayLink?.invalidate()
}
didSet {
guard oldValue !== self.progressObjectDisplayLink else { return }
self.progressObjectDisplayLink?.add(to: .main, forMode: .default)
}
}
private lazy var topSpacer: UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
view.isHidden = true
return view
}()
private lazy var bottomSpacer: UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
view.isHidden = true
return view
}()
}
private extension STProgressHUD {
func commonInit() {
self.alpha = 0
self.isOpaque = false
self.backgroundColor = .clear
self.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.layer.allowsGroupOpacity = false
self.setupViews()
self.updateIndicators()
self.registerForNotifications()
}
func setupViews() {
self.addSubview(self.backgroundView)
self.addSubview(self.bezelView)
self.updateBezelMotionEffects()
for view in [self.label, self.detailsLabel, self.button] as [UIView] {
self.bezelView.addSubview(view)
}
self.bezelView.addSubview(self.topSpacer)
self.bezelView.addSubview(self.bottomSpacer)
}
func showUsingAnimation(_ animated: Bool) {
self.bezelView.layer.removeAllAnimations()
self.backgroundView.layer.removeAllAnimations()
self.hideDelayTimer?.invalidate()
self.showStarted = Date()
self.alpha = 1.0
self.setProgressDisplayLinkEnabled(true)
if animated {
self.animateIn(true, withType: self.animationType, completion: nil)
} else {
self.bezelView.alpha = 1
self.backgroundView.alpha = 1
}
}
func hideUsingAnimation(_ animated: Bool) {
if animated, self.showStarted != nil {
self.showStarted = nil
self.animateIn(false, withType: self.animationType) { [weak self] _ in
self?.done()
}
} else {
self.showStarted = nil
self.bezelView.alpha = 0
self.backgroundView.alpha = 0
self.done()
}
}
func animateIn(_ animatingIn: Bool, withType: HudAnimation, completion: ((Bool) -> Void)?) {
var type = withType
if type == .zoom {
type = animatingIn ? .zoomIn : .zoomOut
}
let small = CGAffineTransform(scaleX: 0.5, y: 0.5)
let large = CGAffineTransform(scaleX: 1.5, y: 1.5)
if animatingIn, self.bezelView.alpha == 0.0 {
self.bezelView.transform = (type == .zoomIn) ? small : large
}
UIView.animate(
withDuration: 0.3,
delay: 0,
usingSpringWithDamping: 1,
initialSpringVelocity: 0,
options: .beginFromCurrentState,
animations: {
if animatingIn {
self.bezelView.transform = .identity
} else if type == .zoomIn {
self.bezelView.transform = large
} else if type == .zoomOut {
self.bezelView.transform = small
}
self.bezelView.alpha = animatingIn ? 1.0 : 0.0
self.backgroundView.alpha = animatingIn ? 1.0 : 0.0
},
completion: completion
)
}
func done() {
self.hideDelayTimer?.invalidate()
self.setProgressDisplayLinkEnabled(false)
if self.isFinished {
self.alpha = 0
if self.removeFromSuperViewOnHide {
self.removeFromSuperview()
}
}
self.completionBlock?()
self.delegate?.hudWasHidden(self)
}
func updateIndicators() {
switch self.mode {
case .indeterminate:
guard !(self.indicator is UIActivityIndicatorView) else { break }
self.indicator?.removeFromSuperview()
let activityIndicator = UIActivityIndicatorView(style: .large)
activityIndicator.startAnimating()
self.indicator = activityIndicator
self.bezelView.addSubview(activityIndicator)
case .determinateHorizontalBar:
self.indicator?.removeFromSuperview()
let barView = STBarProgressView()
self.indicator = barView
self.bezelView.addSubview(barView)
case .determinate:
guard !(self.indicator is STRoundProgressView) else { break }
self.indicator?.removeFromSuperview()
let roundView = STRoundProgressView()
self.indicator = roundView
self.bezelView.addSubview(roundView)
case .annularDeterminate:
guard !(self.indicator is STAnnularProgressView) else { break }
self.indicator?.removeFromSuperview()
let annularView = STAnnularProgressView()
self.indicator = annularView
self.bezelView.addSubview(annularView)
case .customView:
guard let customView = self.customView, customView !== self.indicator else { break }
self.indicator?.removeFromSuperview()
self.indicator = customView
self.bezelView.addSubview(customView)
case .text:
self.indicator?.removeFromSuperview()
self.indicator = nil
}
self.indicator?.translatesAutoresizingMaskIntoConstraints = false
(self.indicator as? STProgressView)?.progress = self.progress
self.indicator?.setContentCompressionResistancePriority(.init(rawValue: 998), for: .horizontal)
self.indicator?.setContentCompressionResistancePriority(.init(rawValue: 998), for: .vertical)
self.updateViews(forColor: self.contentColor)
self.setNeedsUpdateConstraints()
}
func updateViews(forColor color: UIColor) {
self.label.textColor = color
self.detailsLabel.textColor = color
self.button.setTitleColor(color, for: .normal)
if let activityIndicator = self.indicator as? UIActivityIndicatorView {
activityIndicator.color = color
} else if let barProgressView = self.indicator as? STBarProgressView {
barProgressView.progressColor = color
barProgressView.lineColor = color
} else if let circleProgressView = self.indicator as? STCircleProgressView {
circleProgressView.progressTintColor = color
circleProgressView.backgroundTintColor = color.withAlphaComponent(0.1)
}
}
func updateBezelMotionEffects() {
if self.isDefaultMotionEffectsEnabled {
let effectOffset: CGFloat = 10.0
let effectX = UIInterpolatingMotionEffect(keyPath: "center.x", type: .tiltAlongHorizontalAxis)
effectX.maximumRelativeValue = effectOffset
effectX.minimumRelativeValue = -effectOffset
// Fix: effectY 应使用 tiltAlongVerticalAxis
let effectY = UIInterpolatingMotionEffect(keyPath: "center.y", type: .tiltAlongVerticalAxis)
effectY.maximumRelativeValue = effectOffset
effectY.minimumRelativeValue = -effectOffset
let group = UIMotionEffectGroup()
group.motionEffects = [effectX, effectY]
self.bezelView.addMotionEffect(group)
} else {
self.bezelView.motionEffects.forEach { self.bezelView.removeMotionEffect($0) }
}
}
func updatePaddingConstraints() {
var hasVisibleAncestors = false
for padding in self.paddingConstraints {
guard let firstView = padding.firstItem as? UIView,
let secondView = padding.secondItem as? UIView else { continue }
let firstVisible = !firstView.isHidden && firstView.intrinsicContentSize != .zero
let secondVisible = !secondView.isHidden && secondView.intrinsicContentSize != .zero
padding.constant = (firstVisible && (secondVisible || hasVisibleAncestors)) ? self.defaultPadding : 0
hasVisibleAncestors = hasVisibleAncestors || secondVisible
}
}
func apply(priority: UILayoutPriority, toConstraints constraints: [NSLayoutConstraint]) {
constraints.forEach { $0.priority = priority }
}
func setProgressDisplayLinkEnabled(_ enabled: Bool) {
if enabled, self.progressObject != nil {
if self.progressObjectDisplayLink == nil {
self.progressObjectDisplayLink = CADisplayLink(target: self, selector: #selector(updateProgressFromProgressObject))
}
} else {
self.progressObjectDisplayLink = nil
}
}
func registerForNotifications() {
#if !os(tvOS)
NotificationCenter.default.addObserver(
self,
selector: #selector(statusBarOrientationDidChange(_:)),
name: UIDevice.orientationDidChangeNotification,
object: nil
)
#endif
}
func unregisterFromNotifications() {
#if !os(tvOS)
// 与 registerForNotifications() 配对的注销方法,观察者生命周期由注册/注销配对管理。
NotificationCenter.default.removeObserver(self)
#endif
}
}
private extension STProgressHUD {
@objc func handleGraceTimer(_ timer: Timer) {
guard !self.isFinished else { return }
self.showUsingAnimation(self.isUseAnimation)
}
@objc func handleMinShowTimer(_ timer: Timer) {
self.hideUsingAnimation(self.isUseAnimation)
}
@objc func handleHideTimer(_ timer: Timer) {
guard let animation = timer.userInfo as? STHUDAnimation else { return }
self.hide(animation: animation)
}
@objc func updateProgressFromProgressObject() {
guard let fractionCompleted = self.progressObject?.fractionCompleted else { return }
self.progress = Float(fractionCompleted)
}
#if !os(tvOS)
@objc func statusBarOrientationDidChange(_ notification: NSNotification) {
guard self.superview != nil else { return }
self.frame = self.superview?.bounds ?? self.frame
}
#endif
}
public extension STHUDAnimation {
var hudAnimation: STProgressHUD.HudAnimation {
switch self {
case .none, .fade: return .fade
case .zoom: return .zoom
case .zoomIn: return .zoomIn
case .zoomOut: return .zoomOut
}
}
}
public extension STProgressHUD.HudAnimation {
var stHUDAnimation: STHUDAnimation {
switch self {
case .fade: return .fade
case .zoom: return .zoom
case .zoomIn: return .zoomIn
case .zoomOut: return .zoomOut
}
}
}
public class STProgressHUDBackgroundView: UIView {
public enum BackgroundStyle {
case solidColor, blur, liquidGlass
}
public var style: BackgroundStyle? {
didSet { self.updateForBackgroundStyle() }
}
public var color: UIColor? {
didSet {
assert(self.color != nil, "The color should not be nil.")
guard let color = self.color else { return }
self.backgroundColor = color
}
}
private var effectView: UIVisualEffectView?
override init(frame: CGRect) {
super.init(frame: frame)
self.isOpaque = false
self.style = .blur
self.color = UIColor(white: 0.8, alpha: 0.6)
self.clipsToBounds = true
self.updateForBackgroundStyle()
}
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.isOpaque = false
self.style = .blur
self.color = UIColor(white: 0.8, alpha: 0.6)
self.clipsToBounds = true
self.updateForBackgroundStyle()
}
override public var intrinsicContentSize: CGSize { .zero }
private func updateForBackgroundStyle() {
if self.style == .liquidGlass {
self.effectView?.removeFromSuperview()
self.st_enableLiquidGlassBackground(
tintColor: self.color ?? UIColor(white: 0.8, alpha: 0.35),
highlightOpacity: 0.28,
borderColor: UIColor.white.withAlphaComponent(0.32)
)
self.backgroundColor = .clear
self.layer.allowsGroupOpacity = false
} else if self.style == .blur {
self.st_disableLiquidGlassBackground()
self.effectView?.removeFromSuperview()
let effect = UIBlurEffect(style: .light)
let view = UIVisualEffectView(effect: effect)
self.addSubview(view)
view.frame = self.bounds
view.autoresizingMask = [.flexibleHeight, .flexibleWidth]
self.effectView = view
self.backgroundColor = self.color
self.layer.allowsGroupOpacity = false
} else {
self.st_disableLiquidGlassBackground()
self.effectView?.removeFromSuperview()
self.effectView = nil
self.backgroundColor = self.color
self.layer.allowsGroupOpacity = true
}
}
}
class STProgressHUDRoundedButton: UIButton {
override init(frame: CGRect) {
super.init(frame: frame)
self.layer.borderWidth = 1.0
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
self.layer.cornerRadius = self.bounds.height / 2.0
}
override var intrinsicContentSize: CGSize {
guard self.allControlEvents != UIControl.Event(rawValue: 0) else { return .zero }
var size = super.intrinsicContentSize
size.width += 20.0
return size
}
override func setTitleColor(_ color: UIColor?, for state: UIControl.State) {
super.setTitleColor(color, for: state)
self.layer.borderColor = color?.cgColor
let highlighted = self.isHighlighted
self.isHighlighted = highlighted
}
override var isHighlighted: Bool {
didSet {
let baseColor = self.titleColor(for: .selected)
self.backgroundColor = self.isHighlighted ? baseColor?.withAlphaComponent(0.1) : .clear
}
}
}