HUDTools.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //
  2. // HUDTools.swift
  3. // O2Platform
  4. //
  5. // Created by 程剑 on 2017/6/30.
  6. // Copyright © 2017年 zoneland. All rights reserved.
  7. //
  8. import Foundation
  9. import MBProgressHUD
  10. // keyWindow
  11. let KeyWindow : UIWindow = UIApplication.shared.keyWindow!
  12. private var HUDKey = "HUDKey"
  13. extension UIViewController
  14. {
  15. var hud : MBProgressHUD?
  16. {
  17. get{
  18. return objc_getAssociatedObject(self, &HUDKey) as? MBProgressHUD
  19. }
  20. set{
  21. objc_setAssociatedObject(self, &HUDKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  22. }
  23. }
  24. fileprivate static var waveView: O2LoadingView?
  25. /**
  26. 显示提示信息(有菊花, 一直显示, 不消失),默认文字“加载中”,默认偏移量0
  27. - parameter view: 显示在哪个View上
  28. - parameter hint: 提示信息
  29. - parameter yOffset: y上的偏移量
  30. */
  31. func showHud(in view: UIView, hint: String = "加载中...", yOffset:CGFloat? = 0){
  32. let HUD = MBProgressHUD(view: view)
  33. HUD.label.text = hint
  34. //HUD.label.font = UIFontSize(size: 15 * UIRate)
  35. //设为false后点击屏幕其他地方有反应
  36. HUD.isUserInteractionEnabled = true
  37. //HUD内的内容的颜色
  38. //HUD.contentColor = UIColor(red:0.82, green:0.82, blue:0.82, alpha:1.00)
  39. //View的颜色
  40. //HUD.bezelView.color = UIColorHex("000000", 0.7)
  41. //style -blur 不透明 -solidColor 透明
  42. HUD.bezelView.style = .solidColor
  43. HUD.margin = 10.0
  44. //偏移量,以center为起点
  45. // HUD.offset.y = yOffset ?? 0
  46. view.addSubview(HUD)
  47. HUD.show(animated: true)
  48. hud = HUD
  49. }
  50. /**
  51. 显示纯文字提示信息(显示在keywindow上),默认时间2s,默认偏移量0
  52. - parameter hint: 提示信息
  53. - parameter duration: 持续时间(不填的话, 默认两秒)
  54. - parameter yOffset: y上的偏移量
  55. */
  56. func showHintInKeywindow(hint: String, duration: Double = 2.0, yOffset:CGFloat? = 0) {
  57. let view = KeyWindow
  58. let HUD = MBProgressHUD(view: view)
  59. view.addSubview(HUD)
  60. HUD.animationType = .zoomOut
  61. HUD.isUserInteractionEnabled = false
  62. HUD.bezelView.color = UIColor.black
  63. HUD.contentColor = UIColor.white
  64. HUD.mode = .text
  65. HUD.label.text = hint
  66. HUD.show(animated: true)
  67. HUD.removeFromSuperViewOnHide = false
  68. HUD.offset.y = yOffset ?? 0
  69. HUD.margin = 10.0
  70. HUD.hide(animated: true, afterDelay: duration)
  71. hud = HUD
  72. }
  73. /**
  74. 显示纯文字提示信息,默认时间1.5s,默认偏移量0
  75. - parameter view: 显示在哪个View上
  76. - parameter hint: 提示信息
  77. - parameter duration: 持续时间(不填的话, 默认两秒)
  78. - parameter yOffset: y上的偏移量
  79. */
  80. func showHint(in view: UIView, hint: String, duration: Double = 1.5, yOffset:CGFloat? = 0) {
  81. let HUD = MBProgressHUD(view: view)
  82. view.addSubview(HUD)
  83. HUD.animationType = .zoomOut
  84. HUD.bezelView.color = UIColor.black
  85. HUD.contentColor = UIColor.white
  86. HUD.mode = .text
  87. HUD.label.text = hint
  88. HUD.isUserInteractionEnabled = false
  89. HUD.removeFromSuperViewOnHide = false
  90. HUD.show(animated: true)
  91. HUD.offset.y = yOffset ?? 0
  92. HUD.margin = 10.0
  93. HUD.hide(animated: true, afterDelay: duration)
  94. hud = HUD
  95. }
  96. func showSuccess(in view: UIView, hint: String, duration: Double = 1.5, yOffset: CGFloat? = 0){
  97. let HUD = MBProgressHUD(view: view)
  98. view.addSubview(HUD)
  99. HUD.animationType = .zoomOut
  100. HUD.mode = .customView
  101. HUD.customView = UIImageView(image: UIImage(named: "ProgressHUD.bundle/progresshud-success.png"))
  102. HUD.label.text = hint
  103. HUD.isUserInteractionEnabled = false
  104. HUD.removeFromSuperViewOnHide = false
  105. HUD.show(animated: true)
  106. HUD.offset.y = yOffset ?? 0
  107. HUD.margin = 10
  108. HUD.hide(animated: true, afterDelay: duration)
  109. hud = HUD
  110. }
  111. func showError(in view: UIView, hint: String, duration: Double = 1.5, yOffset: CGFloat? = 0){
  112. let HUD = MBProgressHUD(view: view)
  113. view.addSubview(HUD)
  114. HUD.animationType = .zoomOut
  115. HUD.mode = .customView
  116. HUD.customView = UIImageView(image: UIImage(named: "ProgressHUD.bundle/progresshud-error.png"))
  117. HUD.label.text = hint
  118. HUD.isUserInteractionEnabled = false
  119. HUD.removeFromSuperViewOnHide = false
  120. HUD.show(animated: true)
  121. HUD.offset.y = yOffset ?? 0
  122. HUD.margin = 10
  123. HUD.hide(animated: true, afterDelay: duration)
  124. hud = HUD
  125. }
  126. func showLoading(in view: UIView, hint: String, duration: Double = 6, yOffset: CGFloat? = 0){
  127. /*
  128. let HUD = MBProgressHUD(view: view)
  129. view.addSubview(HUD)
  130. HUD.animationType = .zoomOut
  131. HUD.mode = .customView
  132. let wv = O2LoadingView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
  133. HUD.customView = wv
  134. HUD.label.text = hint
  135. HUD.isUserInteractionEnabled = false
  136. HUD.removeFromSuperViewOnHide = false
  137. HUD.show(animated: true)
  138. HUD.offset.y = yOffset ?? 0
  139. HUD.margin = 10
  140. HUD.hide(animated: true, afterDelay: duration)
  141. hud = HUD
  142. UIViewController.waveView = wv
  143. */
  144. let waveV = O2LoadingView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
  145. waveV.center = view.center
  146. view.addSubview(waveV)
  147. }
  148. /// 移除提示
  149. func hideHud() {
  150. //如果解包成功则移除,否则不做任何事
  151. if let hud = hud {
  152. hud.hide(animated: true)
  153. }
  154. }
  155. }