123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- //
- // MCHud.swift
- // HCQuanfangtong
- //
- // Created by Apple on 2022/2/7.
- // Copyright © 2022 Jyp. All rights reserved.
- //
- import UIKit
- var kAppDelegateWindow : UIWindow = (UIApplication.shared.delegate?.window)!!
- /// HUD
- @objcMembers class MCHud: NSObject {
-
- /// 自动消失时间
- static let hudDelay : Float = 1
- static let timeoutDelay : Float = 30
- /// 标签
- private let uuidStr : String = String.init(Date().timeIntervalSince1970)
- /// 存在延时消失的hud
- private var currentHudDelay : Float = 0
-
-
- //MARK: -Loading 加载 默认可以点击后面
- @objc(showLoadingWithMsg: canTouchBack:)
- /// 展示加载hud
- /// - Parameters:
- /// - msg: loading msg
- /// - canTouchBack: 是否可以点击后面
- final func showLoading(_ msg: String = "", canTouchBack: Bool = true){
- let hud = createHud(withMsg: msg, customView: nil)
- hud?.backgroundView.isUserInteractionEnabled = !canTouchBack;
- hud?.isUserInteractionEnabled = !canTouchBack
- hud?.mode = MBProgressHUDMode.indeterminate;
- hud?.show(animated: true)
- hud?.hide(animated: true, afterDelay: TimeInterval(MCHud.timeoutDelay))
- }
-
- //MARK: -Success 成功 ✅
- @objc(showSuccessWithMsg: delay:)
- /// 成功提示
- /// - Parameters:
- /// - msg: 成功提示文字
- /// - delay: 多长时间消失
- final func showSuccess(_ msg: String = LanguagesUtil.createTextBy(Ctext: "操作成功", Etext: "operate success"), delay: Float = MCHud.hudDelay){
- self.currentHudDelay = delay
- let hud = createHud(withMsg: msg, customView: nil)
- hud?.mode = MBProgressHUDMode.text;
- hud?.minShowTime = TimeInterval(delay)
- hud?.show(animated: true)
- hud?.hide(animated: true)
- }
-
- //MARK: -Failure 失败/错误 ❎
- @objc(showFailureWithMsg: delay:)
- /// 失败提示Hud
- /// - Parameters:
- /// - msg: 失败提示文字
- /// - delay: 多长时间消失
- final func showFailure(_ msg: String = LanguagesUtil.createTextBy(Ctext: "操作失败", Etext: "operation failure"), delay: Float = MCHud.hudDelay){
- self.currentHudDelay = delay
- let hud = createHud(withMsg: msg, customView: nil)
- hud?.mode = MBProgressHUDMode.text;
- // hud?.show(animated: true)
- // hud?.hide(animated: true, afterDelay: TimeInterval(delay))
- hud?.minShowTime = TimeInterval(delay)
- hud?.show(animated: true)
- hud?.hide(animated: true)
- }
- //MARK: -Warning 警告 ⚠️
- @objc(showWarningWithMsg: delay:)
- /// 警告提示hud
- /// - Parameters:
- /// - msg: 警告文字
- /// - delay: 多长时间消失
- final func showWarning(_ msg: String, delay: Float = MCHud.hudDelay){
- self.currentHudDelay = delay
- let hud = createHud(withMsg: msg, customView: nil)
- hud?.mode = MBProgressHUDMode.text;
- hud?.minShowTime = TimeInterval(delay)
- hud?.show(animated: true)
- hud?.hide(animated: true)
- }
- //MARK: -Hidden 隐藏
- /// 隐藏hud
- final func hidden(){
- self.currentHudDelay = 0
- MCProgressHud.hide(for: kAppDelegateWindow, animated: true, uuidStr: self.uuidStr, afterDelay: 0)
- }
-
-
- //MARK: -
- private func getAnyHud() -> MCProgressHud?{
- let array = MCProgressHud.allHUDs(for: kAppDelegateWindow, uuidStr: self.uuidStr)
- return array.count > 0 ? array[0]:nil
- }
-
-
- private func createHud(withMsg msg: String?, customView: UIView?) -> MCProgressHud?{
- var progressHud : MCProgressHud? = getAnyHud()
- if progressHud == nil{
- progressHud = MCProgressHud.init(view: kAppDelegateWindow)
- kAppDelegateWindow.addSubview(progressHud!)
- }
- if let hud = progressHud{
- kAppDelegateWindow.bringSubviewToFront(kAppDelegateWindow)
- hud.uuidStr = self.uuidStr
- hud.bezelView.color = UIColor.black
- hud.label.font = UIFont.systemFont(ofSize: 14)
- hud.label.textColor = UIColor.white
- hud.label.numberOfLines = 0
- hud.removeFromSuperViewOnHide = true
- hud.contentColor = UIColor.white
- hud.tintColor = UIColor.white
- hud.isUserInteractionEnabled = false
- hud.label.text = msg
- hud.customView = customView
- if !msg.isEmptyStr{
- hud.margin = 10
- }
- return hud
- }
- return nil
- }
-
- deinit {
- if self.currentHudDelay > 0{
- let uuidStr = self.uuidStr
- DELAY(Double(self.currentHudDelay)) {
- MCProgressHud.hide(for: kAppDelegateWindow, animated: false, uuidStr: uuidStr, afterDelay: 0)
- }
- }
- else{
- MCProgressHud.hide(for: kAppDelegateWindow, animated: false, uuidStr: self.uuidStr, afterDelay: 0)
- }
- }
-
-
- }
- class MCProgressHud: MBProgressHUD {
- var uuidStr : String!
-
- static func hide(for view: UIView, animated: Bool, uuidStr: String, afterDelay: TimeInterval) {
- let huds = MCProgressHud.allHUDs(for: view, uuidStr: uuidStr)
- for hud in huds{
- hud.removeFromSuperViewOnHide = true
- hud.hide(animated: animated, afterDelay: afterDelay)
- }
-
- }
-
- static func allHUDs(for view: UIView, uuidStr: String) -> [MCProgressHud] {
- var huds : [MCProgressHud] = []
- for vi in view.subviews{
- if vi is MCProgressHud && (vi as! MCProgressHud).uuidStr == uuidStr{
- huds.append(vi as! MCProgressHud)
- }
- }
- return huds
-
- }
-
- }
|