MCHud.swift 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //
  2. // MCHud.swift
  3. // HCQuanfangtong
  4. //
  5. // Created by Apple on 2022/2/7.
  6. // Copyright © 2022 Jyp. All rights reserved.
  7. //
  8. import UIKit
  9. var kAppDelegateWindow : UIWindow = (UIApplication.shared.delegate?.window)!!
  10. /// HUD
  11. @objcMembers class MCHud: NSObject {
  12. /// 自动消失时间
  13. static let hudDelay : Float = 1
  14. static let timeoutDelay : Float = 30
  15. /// 标签
  16. private let uuidStr : String = String.init(Date().timeIntervalSince1970)
  17. /// 存在延时消失的hud
  18. private var currentHudDelay : Float = 0
  19. //MARK: -Loading 加载 默认可以点击后面
  20. @objc(showLoadingWithMsg: canTouchBack:)
  21. /// 展示加载hud
  22. /// - Parameters:
  23. /// - msg: loading msg
  24. /// - canTouchBack: 是否可以点击后面
  25. final func showLoading(_ msg: String = "", canTouchBack: Bool = true){
  26. let hud = createHud(withMsg: msg, customView: nil)
  27. hud?.backgroundView.isUserInteractionEnabled = !canTouchBack;
  28. hud?.isUserInteractionEnabled = !canTouchBack
  29. hud?.mode = MBProgressHUDMode.indeterminate;
  30. hud?.show(animated: true)
  31. hud?.hide(animated: true, afterDelay: TimeInterval(MCHud.timeoutDelay))
  32. }
  33. //MARK: -Success 成功 ✅
  34. @objc(showSuccessWithMsg: delay:)
  35. /// 成功提示
  36. /// - Parameters:
  37. /// - msg: 成功提示文字
  38. /// - delay: 多长时间消失
  39. final func showSuccess(_ msg: String = LanguagesUtil.createTextBy(Ctext: "操作成功", Etext: "operate success"), delay: Float = MCHud.hudDelay){
  40. self.currentHudDelay = delay
  41. let hud = createHud(withMsg: msg, customView: nil)
  42. hud?.mode = MBProgressHUDMode.text;
  43. hud?.minShowTime = TimeInterval(delay)
  44. hud?.show(animated: true)
  45. hud?.hide(animated: true)
  46. }
  47. //MARK: -Failure 失败/错误 ❎
  48. @objc(showFailureWithMsg: delay:)
  49. /// 失败提示Hud
  50. /// - Parameters:
  51. /// - msg: 失败提示文字
  52. /// - delay: 多长时间消失
  53. final func showFailure(_ msg: String = LanguagesUtil.createTextBy(Ctext: "操作失败", Etext: "operation failure"), delay: Float = MCHud.hudDelay){
  54. self.currentHudDelay = delay
  55. let hud = createHud(withMsg: msg, customView: nil)
  56. hud?.mode = MBProgressHUDMode.text;
  57. // hud?.show(animated: true)
  58. // hud?.hide(animated: true, afterDelay: TimeInterval(delay))
  59. hud?.minShowTime = TimeInterval(delay)
  60. hud?.show(animated: true)
  61. hud?.hide(animated: true)
  62. }
  63. //MARK: -Warning 警告 ⚠️
  64. @objc(showWarningWithMsg: delay:)
  65. /// 警告提示hud
  66. /// - Parameters:
  67. /// - msg: 警告文字
  68. /// - delay: 多长时间消失
  69. final func showWarning(_ msg: String, delay: Float = MCHud.hudDelay){
  70. self.currentHudDelay = delay
  71. let hud = createHud(withMsg: msg, customView: nil)
  72. hud?.mode = MBProgressHUDMode.text;
  73. hud?.minShowTime = TimeInterval(delay)
  74. hud?.show(animated: true)
  75. hud?.hide(animated: true)
  76. }
  77. //MARK: -Hidden 隐藏
  78. /// 隐藏hud
  79. final func hidden(){
  80. self.currentHudDelay = 0
  81. MCProgressHud.hide(for: kAppDelegateWindow, animated: true, uuidStr: self.uuidStr, afterDelay: 0)
  82. }
  83. //MARK: -
  84. private func getAnyHud() -> MCProgressHud?{
  85. let array = MCProgressHud.allHUDs(for: kAppDelegateWindow, uuidStr: self.uuidStr)
  86. return array.count > 0 ? array[0]:nil
  87. }
  88. private func createHud(withMsg msg: String?, customView: UIView?) -> MCProgressHud?{
  89. var progressHud : MCProgressHud? = getAnyHud()
  90. if progressHud == nil{
  91. progressHud = MCProgressHud.init(view: kAppDelegateWindow)
  92. kAppDelegateWindow.addSubview(progressHud!)
  93. }
  94. if let hud = progressHud{
  95. kAppDelegateWindow.bringSubviewToFront(kAppDelegateWindow)
  96. hud.uuidStr = self.uuidStr
  97. hud.bezelView.color = UIColor.black
  98. hud.label.font = UIFont.systemFont(ofSize: 14)
  99. hud.label.textColor = UIColor.white
  100. hud.label.numberOfLines = 0
  101. hud.removeFromSuperViewOnHide = true
  102. hud.contentColor = UIColor.white
  103. hud.tintColor = UIColor.white
  104. hud.isUserInteractionEnabled = false
  105. hud.label.text = msg
  106. hud.customView = customView
  107. if !msg.isEmptyStr{
  108. hud.margin = 10
  109. }
  110. return hud
  111. }
  112. return nil
  113. }
  114. deinit {
  115. if self.currentHudDelay > 0{
  116. let uuidStr = self.uuidStr
  117. DELAY(Double(self.currentHudDelay)) {
  118. MCProgressHud.hide(for: kAppDelegateWindow, animated: false, uuidStr: uuidStr, afterDelay: 0)
  119. }
  120. }
  121. else{
  122. MCProgressHud.hide(for: kAppDelegateWindow, animated: false, uuidStr: self.uuidStr, afterDelay: 0)
  123. }
  124. }
  125. }
  126. class MCProgressHud: MBProgressHUD {
  127. var uuidStr : String!
  128. static func hide(for view: UIView, animated: Bool, uuidStr: String, afterDelay: TimeInterval) {
  129. let huds = MCProgressHud.allHUDs(for: view, uuidStr: uuidStr)
  130. for hud in huds{
  131. hud.removeFromSuperViewOnHide = true
  132. hud.hide(animated: animated, afterDelay: afterDelay)
  133. }
  134. }
  135. static func allHUDs(for view: UIView, uuidStr: String) -> [MCProgressHud] {
  136. var huds : [MCProgressHud] = []
  137. for vi in view.subviews{
  138. if vi is MCProgressHud && (vi as! MCProgressHud).uuidStr == uuidStr{
  139. huds.append(vi as! MCProgressHud)
  140. }
  141. }
  142. return huds
  143. }
  144. }