ZLProgressHUD.swift 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //
  2. // ZLProgressHUD.swift
  3. // ZLPhotoBrowser
  4. //
  5. // Created by long on 2020/8/17.
  6. //
  7. // Copyright (c) 2020 Long Zhang <495181165@qq.com>
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. import UIKit
  27. public class ZLProgressHUD: UIView {
  28. @objc public enum HUDStyle: Int {
  29. case light
  30. case lightBlur
  31. case dark
  32. case darkBlur
  33. func bgColor() -> UIColor {
  34. switch self {
  35. case .light:
  36. return .white
  37. case .dark:
  38. return .darkGray
  39. default:
  40. return .clear
  41. }
  42. }
  43. func textColor() -> UIColor {
  44. switch self {
  45. case .light, .lightBlur:
  46. return .black
  47. case .dark, .darkBlur:
  48. return .white
  49. }
  50. }
  51. func indicatorStyle() -> UIActivityIndicatorView.Style {
  52. switch self {
  53. case .light, .lightBlur:
  54. return .gray
  55. case .dark, .darkBlur:
  56. return .white
  57. }
  58. }
  59. func blurEffectStyle() -> UIBlurEffect.Style? {
  60. switch self {
  61. case .light, .dark:
  62. return nil
  63. case .lightBlur:
  64. return .extraLight
  65. case .darkBlur:
  66. return .dark
  67. }
  68. }
  69. }
  70. let style: ZLProgressHUD.HUDStyle
  71. var timeoutBlock: ( () -> Void )?
  72. var timer: Timer?
  73. deinit {
  74. self.cleanTimer()
  75. }
  76. @objc public init(style: ZLProgressHUD.HUDStyle) {
  77. self.style = style
  78. super.init(frame: UIScreen.main.bounds)
  79. self.setupUI()
  80. }
  81. required init?(coder: NSCoder) {
  82. fatalError("init(coder:) has not been implemented")
  83. }
  84. func setupUI() {
  85. let view = UIView(frame: CGRect(x: 0, y: 0, width: 110, height: 90))
  86. view.layer.masksToBounds = true
  87. view.layer.cornerRadius = 5.0
  88. view.backgroundColor = self.style.bgColor()
  89. view.clipsToBounds = true
  90. view.alpha = 0.8
  91. view.center = self.center
  92. if self.style == .lightBlur || self.style == .darkBlur {
  93. let effect = UIBlurEffect(style: self.style.blurEffectStyle()!)
  94. let effectView = UIVisualEffectView(effect: effect)
  95. effectView.frame = view.bounds
  96. view.addSubview(effectView)
  97. }
  98. let indicator = UIActivityIndicatorView(style: self.style.indicatorStyle())
  99. indicator.frame = CGRect(x: (view.bounds.width - indicator.bounds.width)/2, y: 18, width: indicator.bounds.width, height: indicator.bounds.height)
  100. indicator.startAnimating()
  101. view.addSubview(indicator)
  102. let label = UILabel(frame: CGRect(x: 0, y: 50, width: view.bounds.width, height: 30))
  103. label.textAlignment = .center
  104. label.textColor = self.style.textColor()
  105. label.font = getFont(16)
  106. label.text = localLanguageTextValue(.hudLoading)
  107. view.addSubview(label)
  108. self.addSubview(view)
  109. }
  110. @objc public func show(timeout: TimeInterval = 100) {
  111. DispatchQueue.main.async {
  112. UIApplication.shared.keyWindow?.addSubview(self)
  113. }
  114. if timeout > 0 {
  115. self.cleanTimer()
  116. self.timer = Timer.scheduledTimer(timeInterval: timeout, target: ZLWeakProxy(target: self), selector: #selector(timeout(_:)), userInfo: nil, repeats: false)
  117. RunLoop.current.add(self.timer!, forMode: .default)
  118. }
  119. }
  120. @objc public func hide() {
  121. self.cleanTimer()
  122. DispatchQueue.main.async {
  123. self.removeFromSuperview()
  124. }
  125. }
  126. @objc func timeout(_ timer: Timer) {
  127. self.timeoutBlock?()
  128. self.hide()
  129. }
  130. func cleanTimer() {
  131. self.timer?.invalidate()
  132. self.timer = nil
  133. }
  134. }