天天看點

iOS swift 給MBProgressHUD添加分類

MBProgressHUD在開發中經常會用到,今天把MBProgressHUD的方法拓展了一下,更加友善使用

1.可以實作gif圖檔的展示,使用時請替換test.gif

2.可以控制是否允許互動,如果允許互動,那麼在彈窗期間界面不可以點選

3.更便捷的控制succss和error的提示,使用時,請替換對應的success.png,error.png

4.所有參數都集中在show方法中,參數都是可以選的,最簡單的顯示一個彈窗僅需MBProgressHUD.show()

import Foundation

extension MBProgressHUD {
    
    /// MBProgressHUD gif顯示
    ///
    /// - Parameters:
    ///   - view: view default -> UIWindow
    ///   - disableInteraction: 是否使能互動
    ///   - animated: 動畫 true
    static func showGif(to view:UIView? = nil,disableInteraction:Bool = true,animated:Bool = true){
        //如果是gif可以使用sdwebImage的方法加載本地gif
        let path = Bundle.main.path(forResource: "test", ofType: "gif")
        let data = NSData(contentsOfFile: path ?? "") as Data?
        guard let image = UIImage.sd_animatedGIF(with: data) else{
            fatalError("gif圖檔加載失敗");
        }
        let giftImgView = UIImageView(image: image)
        let hud = MBProgressHUD.showHudAdded(to: view, animated: animated)
        hud?.color = .clear
        hud?.mode = .customView
        hud?.isUserInteractionEnabled = disableInteraction
        hud?.customView = giftImgView
    }
    
    /// 拓展MBProgressHUD顯示方法
    ///
    /// - Parameters:
    ///   - message: text
    ///   - icon: picture
    ///   - view: view default->UIwindow
    ///   - disableInteraction: 是否使能互動
    ///   - afterDelay: 延時 預設0
    ///   - animated: 動畫 true
    static func show(message:String? = nil ,
                     icon:String? = nil ,
                     to view:UIView? = nil,
                     disableInteraction:Bool = true,
                     afterDelay:TimeInterval = 0,
                     animated:Bool = true){
        
        let hud = self.showHudAdded(to: view, animated: true)
        hud?.isUserInteractionEnabled = disableInteraction
        hud?.labelText = message
        if let image = UIImage(named: "MBProgressHUD.bundle/\(icon ?? "")") {
            let imgView = UIImageView(image: image)
            hud?.customView = imgView
            hud?.mode = .customView
        }
        if afterDelay > 0.0 {
            hud?.hide(true, afterDelay: afterDelay)
        }
    }
    
    static func showSuccess(message:String = "",to view:UIView? = nil){
        show(message: message, icon: "success.png", to: view ,afterDelay: 2.0)
    }
    
    static func showError(message:String = "",to view:UIView? = nil){
        show(message: message, icon: "error.png", to: view ,afterDelay: 2.0)
    }

    /// 移除keywindow的hud
    static func hide(){
       let v = UIApplication.shared.windows.last;
       hide(for: v, animated: true)
    }
    
    private  static func showHudAdded(to view:UIView? = nil,animated:Bool = true) -> MBProgressHUD?{
        var v = view
        if v == nil {
            v = UIApplication.shared.windows.last;
        }
        hide(for: v, animated: true)
        let hud = MBProgressHUD.showAdded(to: v, animated: animated);
        hud?.dimBackground = false
        hud?.removeFromSuperViewOnHide = true
        return hud

    }
}
           

轉載請标注來源:https://www.cnblogs.com/qqcc1388/p/9851750.html

轉載于:https://www.cnblogs.com/qqcc1388/p/9851750.html

繼續閱讀