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