前言:
MBProgressHUD,是iOS开发中很经典的一个框架,我们可能每个项目,乃至每个ViewController都能用到,越是常用的控件,就更应该简化其调用方式,来优化我们自己的代码.这里我列举出自己常用封装,也建议各位将自己常用的模式也封装起来.
实现效果:
对调用方法进行封装,实现一行代码弹出HUD
实现五种形式:
- 背景半透明显示文字
- 背景透明显示文字
- 背景半透明显示Loading
- 背景透明显示Loading
- 背景透明显示自定义icon+文字
实现方式:
1.新建一个类,并实现创建HUD实例方法.
2.实现显示HUD方法.
3.展示loadingView方法.
4.类Toast只展示文字方法.
5.展示成功,失败类似icon+文字方法.
6.隐藏HUD方法
1.新建一个类,并实现创建HUD实例方法.
static var hud : MBProgressHUD?
/// 创建Hud
///
/// - Parameters:
/// - view: 加载到哪个View展示.
/// - isMask: 是否是蒙层形式,背景半透明.
private class func createHud(view : UIView? = UIApplication.shared.keyWindow, isMask : Bool = false) -> MBProgressHUD? {
guard let supview = view ?? UIApplication.shared.keyWindow else {return nil}
let HUD = MBProgressHUD.showAdded(to: supview
, animated: true)
/// 默认不遮盖navigationBar
HUD.frame = CGRect(x: 0, y: EWScreenInfo.navigationHeight, width: EWScreenInfo.Width, height: EWScreenInfo.Height - EWScreenInfo.navigationHeight)
HUD.animationType = .zoom
if isMask {
/// 蒙层type,背景半透明.
HUD.backgroundView.color = UIColor(white: 0.0, alpha: 0.4)
} else {
/// 非蒙层type,没有背景.
HUD.backgroundView.color = UIColor.clear
HUD.bezelView.backgroundColor = UIColor(white: 0.0, alpha: 0.9)
HUD.contentColor = UIColor.white
}
HUD.removeFromSuperViewOnHide = true
HUD.show(animated: true)
return HUD
}
2.实现显示HUD方法.
/// 直接展示Hud
///
/// - Parameters:
/// - message: 显示信息
/// - icon: 显示图片
/// - view: 加载到哪个View上展示
/// - completeBlock: HUD消失后调用block
private class func showHudTips (message : String?, icon : String?, view : UIView?, completeBlock : (()->(Void))?) {
let HUD = self.createHud(view: view, isMask: false)
HUD?.label.text = message
HUD?.label.numberOfLines = 0
/// 如果有Icon展示.
if let icon = icon {
HUD?.customView = UIImageView.init(image: UIImage.init(named: "\(icon)"))
}
HUD?.mode = .customView
/// 在hud消失时调用completeBlock.
DispatchQueue.main.asyncAfter(deadline: .now() + hudShowTime) {
HUD?.hide(animated: true)
guard let completeBlock = completeBlock else {
return
}
completeBlock()
}
}
3.展示loadingView方法.
两个方法,返回实例方法可以根据自己需求修改返回的实例.
调用展示loadingView时,必须在结束loading时手动调用隐藏HUD方法!!!
/// 展示loadingHUD,可用于网络请求
class func showLoadingHudView (view : UIView? = UIApplication.shared.keyWindow ,message:String?, isMask : Bool = false){
let HUD = self.createHud(view: view, isMask: isMask)
HUD?.mode = .indeterminate
HUD?.label.text = message
hud = HUD
}
/// 生成实例的loadingHUD,可以再对实例进行自定义修改
class func showLoadingHudInView (view : UIView? = UIApplication.shared.keyWindow ,message:String?, isMask : Bool = false) -> MBProgressHUD?{
let HUD = self.createHud(view: view, isMask: isMask)
HUD?.mode = .indeterminate
HUD?.label.text = message
hud = HUD
return hud
}
4.类Toast只展示文字方法.
/// 展示类ToastHUD
class func showTextHudTips(message : String?, view : UIView? = UIApplication.shared.keyWindow , isMask : Bool = false,afterDelay:Double = hudShowTime) {
let HUD = self.createHud(view: view, isMask: isMask)
HUD?.mode = .text
HUD?.detailsLabel.font = UIFont.systemFont(ofSize: 16.0)
HUD?.detailsLabel.text = message
HUD?.hide(animated: true, afterDelay: afterDelay)
}
/// 生成实例的类ToastHUD,可以再对实例进行自定义修改
class func showTextHud (message : String?, view : UIView? = UIApplication.shared.keyWindow , isMask : Bool = false,afterDelay:Double = hudShowTime) -> MBProgressHUD? {
let HUD = self.createHud(view: view, isMask: isMask)
HUD?.mode = .text
HUD?.detailsLabel.font = UIFont.systemFont(ofSize: 16.0)
HUD?.detailsLabel.text = message
HUD?.hide(animated: true, afterDelay: afterDelay)
return HUD
}
5.展示成功,失败类似icon+文字方法.
本质上是icon+文字形式,传入自己项目图片,建议大小为64X64
// 展示成功HUD,可自己修改图片,icon图片需要自己添加到项目中,建议大小为64X64
class func showSuccesshTips (message : String?, view : UIView? = UIApplication.shared.keyWindow ,afterDelay:Double = hudShowTime) {
self.showHudTips(message: message, icon: "Success.png", view: view, completeBlock: nil)
}
// 展示成功HUD,可传入HUD隐藏后调用Block
class func showSuccesshTips (message : String?, view : UIView? = UIApplication.shared.keyWindow , completeBlock : (()->(Void))?) {
self.showHudTips(message: message, icon: "success.png", view: view, completeBlock: completeBlock)
}
// 展示失败HUD,可自己修改图片,icon图片需要自己添加到项目中,建议大小为64X64
class func showErrorMessage(message : String?, view : UIView? = UIApplication.shared.keyWindow ){
self.showHudTips(message: message, icon: "error.png", view: view, completeBlock: nil)
}
// 展示失败HUD,可传入HUD隐藏后调用Block
class func showErrorMessage (message : String?, view : UIView? = UIApplication.shared.keyWindow , completeBlock : (()->(Void))?) {
self.showHudTips(message: message, icon: "error.png", view: view, completeBlock: nil)
}
6.隐藏HUD方法
/// 隐藏HUD
class func hideHud () {
guard let HUD = hud else {
return
}
HUD.hide(animated: true)
hud = nil
}
使用方法示例:
1.使用CocoaPods或手动将MBProgressHUD加入项目.
2.将EWMBProgressHUD.swift文件加入项目.
3.调用时:
@objc private func onClickTopButton(){
EWMBProgressHud.showTextHudTips(message: "蒙层", view: self.view, isMask: true, afterDelay: 1)
}
@objc private func onClickCenterButton(){
EWMBProgressHud.showTextHudTips(message: "Toast")
}
@objc private func onClickLoadingButton(){
EWMBProgressHud.showLoadingHudView(message: "Loading")
/// loadingView必需手动隐藏!!!结束loading请调用hideHud()!!!
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2) {
EWMBProgressHud.hideHud()
}
}
@objc private func onClickSecondLoadingButton(){
EWMBProgressHud.showLoadingHudView(view: self.view, message: "蒙层Loading", isMask: true)
/// loadingView必需手动隐藏!!!结束loading请调用hideHud()!!!
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2) {
EWMBProgressHud.hideHud()
}
}
@objc private func onClickSuccessButton(){
EWMBProgressHud.showSuccesshTips(message: "Success")
}
demo地址:EWJudgmentString
有问题欢迎探讨.
网友评论