• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

iOSswift给MBProgressHUD添加分类

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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

    }
}


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap