// // MPHUD.swift // MusicPlayer // // Created by Mr.Zhou on 2024/4/11. // import UIKit import SVProgressHUD ///二次封装hud class MP_HUD: NSObject { //HUD输出状态 enum status { ///成功 case success ///失败 case error ///只显示文字 case onlyText ///过程 case progress ///加载数据 case loading } ///文本HUD static func text(_ text:String?,delay:TimeInterval,completion:(() -> Void)?){ showWithStatus(hudStatus: .onlyText, text: text, delay: delay, completion: completion) } ///等待HUD static func progress(_ text:String?,delay:TimeInterval,completion:(() -> Void)?){ showWithStatus(hudStatus: .progress, text: text, delay: delay, completion: completion) } ///数据HUD static func loading(_ delay:TimeInterval,completion:(() -> Void)?){ showWithStatus(hudStatus: .loading, text: nil, delay: delay, completion: completion) } ///成功HUD static func success(_ text:String?,delay:TimeInterval,completion:(() -> Void)?){ showWithStatus(hudStatus: .success, text: text, delay: delay, completion: completion) } ///错误HUD static func error(_ text:String?,delay:TimeInterval,completion:(() -> Void)?){ showWithStatus(hudStatus: .error, text: text, delay: delay, completion: completion) } //立刻隐藏当前HUD static func hideNow() { SVProgressHUD.dismiss() } /// 展示HUD /// - Parameters: /// - status: HUD状态 /// - text: 显示文本 /// - delay: 显示时常 static func showWithStatus(hudStatus status: status, text: String?, delay: TimeInterval ,completion:(() -> Void)?) { SVProgressHUD.setDefaultStyle(.light) SVProgressHUD.setDefaultMaskType(.clear) SVProgressHUD.setBackgroundColor(.white) SVProgressHUD.setOffsetFromCenter(.init(horizontal: 0, vertical: 0)) switch status { case .success: SVProgressHUD.showSuccess(withStatus: text) SVProgressHUD.setMinimumSize(CGSize(width: 100 * width, height: 80 * width)) case .error: SVProgressHUD.showError(withStatus: text) case .onlyText: SVProgressHUD.setOffsetFromCenter(.init(horizontal: 0, vertical: (screen_Height / 2) - 85 * width)) SVProgressHUD.setMinimumSize(CGSize(width: 100 * width, height: 40 * width)) SVProgressHUD.show(UIImage(), status: text) case .loading: SVProgressHUD.setBackgroundColor(.white) SVProgressHUD.show() default: SVProgressHUD.show(withStatus: text) } SVProgressHUD.dismiss(withDelay: delay) { guard let completion = completion else{ return } completion() } } }