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

最新文章

  1. Thinkphp框架回顾(三)之怎么实现平常的sql操作数据库
  2. Mac 配置Charles抓https的包
  3. Discovering versions from the identity service failed when creating the password plugin.
  4. 11g Physical Standby配置
  5. HttpWebRequest后台读取网页类
  6. yii框架中应用jquery表单验证插件
  7. NetworkComms框架介绍 完美支持TCP/UDP协议
  8. Objective-c中@interface、@implementation、@protocal
  9. Arrays.asList()
  10. tbl.js div实现的表格控件,完全免费,不依赖jquery
  11. destoon源码分析一
  12. activeMq的入门程序
  13. 干货——详解Java中的关键字
  14. react-native中的props
  15. JS高级 - 面向对象4(json方式面向对象)
  16. 洛谷 P1015 回文数
  17. 计划任务at、crontab
  18. windows平台,实现录音功能详解
  19. 关于graham扫描法求凸包的小记
  20. Appium——unknown error: cannot activate web view

热门文章

  1. springMVC笔记:@ResponseBody
  2. shell spool
  3. python数据类型学习心得
  4. C++ 如何得到当前进程所占用的内存呢?【转】
  5. BindDepthStencilState
  6. C#字符串操作大全
  7. 搜索引擎Solr-6.6.0搭建
  8. Java高级特性—反射和动态代理
  9. 13 Basic Cat Command Examples in Linux
  10. 转:敏捷方式scrum 方案