import UIKit
class LYBHUDExtention: NSObject {
}
extension MBProgressHUD
{
/**
parama1:显示的主标题
parama2:iconArr----图片名数组
parama3:多长时间后消失
*/
fileprivate class func showTextWithCustomAnimate(text: String, iconArr: [String],delay:Double) {
let view = viewWithShow()
let hud = MBProgressHUD.showAdded(to: view, animated: true)
hud.label.text=text
//自定义的view是动画
let imageV=UIImageView.init(frame: CGRect.init(x: 0, y: 0, width: 200, height: 100))
var muimageArr:[UIImage]=[]
for i in 1...iconArr.count{
let image=UIImage.init(named: iconArr[i])
muimageArr.append(image!)
}
imageV.animationImages=muimageArr
imageV.animationDuration=5
imageV.startAnimating()
hud.customView=imageV
//hud显示的大小
hud.minSize=CGSize.init(width: 200, height: 200)
//hud动画的模式
hud.animationType=MBProgressHUDAnimation.zoomIn
hud.mode=MBProgressHUDMode.customView;
hud.removeFromSuperViewOnHide = true
//延迟隐藏
hud.hide(animated: true, afterDelay: delay)
}
//自定义view
fileprivate class func showTextWithCustomView(text: String, icon: String,delay:Double) {
let view = viewWithShow()
let hud = MBProgressHUD.showAdded(to: view, animated: true)
hud.label.text=text
//
let img = UIImage(named: icon)
hud.customView = UIImageView(image: img)
//hud显示的大小
hud.minSize=CGSize.init(width: 200, height: 200)
//hud动画的模式
hud.animationType=MBProgressHUDAnimation.zoomIn
hud.mode=MBProgressHUDMode.customView;
hud.removeFromSuperViewOnHide = true
//延迟隐藏
hud.hide(animated: true, afterDelay: delay)
}
//普通的带转圈
fileprivate class func showText(text: String,delay:Double) {
let view = viewWithShow()
let hud = MBProgressHUD.showAdded(to: view, animated: true)
hud.label.text=text
//hud显示的大小
hud.minSize=CGSize.init(width: 200, height: 200)
//hud动画的模式
hud.animationType=MBProgressHUDAnimation.zoomIn
hud.mode=MBProgressHUDMode.indeterminate;//带动画,默认值
hud.removeFromSuperViewOnHide = true
//延迟隐藏
hud.hide(animated: true, afterDelay: delay)
// //全部隐藏
// hud.hide(animated: true)
// MBProgressHUD.hide(for: view, animated: true)
}
//成功的提示
class func showSuccess(_ text: String) {
showTextWithCustomView(text: text, icon: "hud_done", delay: 2)
}
//失败的提示
class func showError(_ text: String) {
showTextWithCustomView(text: text, icon: "hud_fail", delay: 2)
}
//获取主window
class func viewWithShow() -> UIView {
var window = UIApplication.shared.keyWindow
if window?.windowLevel != UIWindow.Level.normal {
let windowArray = UIApplication.shared.windows
for tempWin in windowArray {
if tempWin.windowLevel == UIWindow.Level.normal {
window = tempWin;
break
}
}
}
return window!
}
//分开使用
class func showStatusInfo(_ info: String) {
let view = viewWithShow()
let hud = MBProgressHUD.showAdded(to: view, animated: true)
hud.minSize=CGSize.init(width: 200, height: 200)
hud.label.text = info
}
//分开使用,隐藏hud
class func dismiss() {
let view = viewWithShow()
MBProgressHUD.hide(for: view, animated: true)
}
}
==============hud的基本使用===============
//类方法
class func showHudWith(text:String,icon:String,view:UIView,delay:Double){
let hud = MBProgressHUD.showAdded(to: view, animated: true)
hud.label.text=text
hud.detailsLabel.text="子标题"
//hud所在view的背景
hud.backgroundColor=UIColor.red;
//菊花和文字的颜色
hud.contentColor=UIColor.green;
//暂时没看见效果
hud.tintColor=UIColor.blue;
//子标题的大小
hud.detailsLabel.font=UIFont.systemFont(ofSize: 15)
//主标题的大小
hud.label.font=UIFont.systemFont(ofSize: 15)
//hud显示的大小
hud.minSize=CGSize.init(width: 200, height: 200)
//hud动画的模式
hud.animationType=MBProgressHUDAnimation.zoomIn
/**
hud显示的模式:
hud.mode = MBProgressHUDMode.customView//自定义view
hud.mode=MBProgressHUDMode.determinate;//不带动画,显示动画图片
hud.mode=MBProgressHUDMode.text;// 不带动画,只带文字
hud.mode=MBProgressHUDMode.indeterminate;//带动画,默认值
*/
hud.mode=MBProgressHUDMode.indeterminate;//带动画,默认值
/**
显示自定义的view,只有在 hud.mode = MBProgressHUDMode.customView的时候才起作用
*/
let img = UIImage(named: icon)
hud.customView = UIImageView(image: img)
/**
自定义的view是动画
*/
let imageV=UIImageView.init(frame: CGRect.init(x: 0, y: 0, width: 200, height: 100))
var muimageArr:[UIImage]=[]
for i in 1...4{
let image=UIImage.init(named: String.init(format: "image%d", i))
muimageArr.append(image!)
}
imageV.animationImages=muimageArr
imageV.animationDuration=5
imageV.startAnimating()
hud.customView=imageV
/***
隐藏的时候移除
*/
hud.removeFromSuperViewOnHide = true
//延迟隐藏
hud.hide(animated: true, afterDelay: delay)
//隐藏所有的hud
hud.hide(animated: true)
}
===========================
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <MBProgressHUD/MBProgressHUD.h>
@interface LYBMBProgressHud : NSObject
+(instancetype)sharedManager;
//取消带动画的提示
-(void)dimisAnimalProgressWith:(UIView *)view;
//文字标题
-(void)showProgresshudAnimalWith:(UIView *)view title:(NSString *)title detailtitle:(NSString *)detailTitle isOnDismissbg:(BOOL)ison hideAfterdelay:(BOOL)hideAfterdelay;
//无动画有标题,自定义View
-(void)showProgresshudTextCustomView:(UIView *)view title:(NSString *)title detailtitle:(NSString *)detailTitle isOnDismissbg:(BOOL)ison hideAfterdelay:(BOOL)hideAfterdelay;
//无动画有标题
-(void)showProgresshudTextWithNOCustomView:(UIView *)view title:(NSString *)title detailtitle:(NSString *)detailTitle isOnDismissbg:(BOOL)ison hideAfterdelay:(BOOL)hideAfterdelay;
@end
*************************
#import "LYBMBProgressHud.h"
@implementation LYBMBProgressHud
+(instancetype)sharedManager{
static LYBMBProgressHud *lybMBProgressHud;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
lybMBProgressHud =[[LYBMBProgressHud alloc]init];
});
return lybMBProgressHud;
}
//取消带转圈的动画
-(void)dimisAnimalProgressWith:(UIView *)view{
[MBProgressHUD hideHUDForView:view animated:YES];
}
//带动画带标题
-(void)showProgresshudAnimalWith:(UIView *)view title:(NSString *)title detailtitle:(NSString *)detailTitle isOnDismissbg:(BOOL)ison hideAfterdelay:(BOOL)hideAfterdelay{
if(view){
MBProgressHUD *hud=[[MBProgressHUD alloc]initWithView:view];
[view addSubview:hud];
hud.mode=MBProgressHUDModeIndeterminate;
hud.detailsLabelFont = [UIFont systemFontOfSize:17];
hud.labelColor=[UIColor whiteColor];
hud.color=[UIColor colorWithWhite:0.8 alpha:0.8];
hud.minSize=CGSizeMake(200, 100);
hud.animationType=MBProgressHUDAnimationZoomIn;
if(title && ![title isEqualToString:@""]){
hud.labelText=title;
}
if(detailTitle && ![detailTitle isEqualToString:@""]){
hud.detailsLabelText=detailTitle;
}
if(ison){
hud.dimBackground=YES;
}
[hud showAnimated:YES];
if(hideAfterdelay){
[hud hideAnimated:YES afterDelay:2];
}
}
}
//无动画有标题自定义View
-(void)showProgresshudTextCustomView:(UIView *)view title:(NSString *)title detailtitle:(NSString *)detailTitle isOnDismissbg:(BOOL)ison hideAfterdelay:(BOOL)hideAfterdelay{
if(view){
MBProgressHUD *hud=[[MBProgressHUD alloc]initWithView:view];
[view addSubview:hud];
hud.mode= MBProgressHUDModeCustomView;
UILabel *lbl=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 100)];
lbl.font=[UIFont systemFontOfSize:15];
lbl.textColor=[UIColor whiteColor];
lbl.textAlignment=NSTextAlignmentCenter;
lbl.numberOfLines=0;
hud.customView=lbl;
hud.color=[UIColor colorWithWhite:0.8 alpha:0.8];
hud.minSize=CGSizeMake(200, 100);
hud.animationType=MBProgressHUDAnimationZoomIn;
if(title && ![title isEqualToString:@""]){
lbl.text=title;
}
if(ison){
hud.dimBackground=YES;
}
[hud show:YES];
if(hideAfterdelay){
[hud hide:YES afterDelay:2];
}
}
}
//无动画有标题
-(void)showProgresshudTextWithNOCustomView:(UIView *)view title:(NSString *)title detailtitle:(NSString *)detailTitle isOnDismissbg:(BOOL)ison hideAfterdelay:(BOOL)hideAfterdelay {
if(view){
MBProgressHUD *hud=[[MBProgressHUD alloc]initWithView:view];
[view addSubview:hud];
hud.mode= MBProgressHUDModeText;
hud.labelFont=[UIFont systemFontOfSize:15];
hud.detailsLabelFont = [UIFont systemFontOfSize:15];
hud.labelColor=[UIColor whiteColor];
hud.color=[UIColor colorWithWhite:0.8 alpha:0.8];
hud.minSize=CGSizeMake(200, 100);
hud.animationType=MBProgressHUDAnimationZoomIn;
if(title && ![title isEqualToString:@""]){
hud.labelText=title;
}
if(detailTitle && ![detailTitle isEqualToString:@""]){
hud.detailsLabelText=detailTitle;
}
if(ison){
hud.dimBackground=YES;
}
[hud show:YES];
if(hideAfterdelay){
[hud hide:YES afterDelay:2];
}
}
}
@end
************************
桥接文件中加入
#import "LYBMBProgressHud.h"
*********使用:
//使用系统的页面
LYBMBProgressHud.sharedManager().showProgresshudText(withNOCustomView: view, title: "不是自定义的View", detailtitle: "", isOnDismissbg: false, hideAfterdelay: true)
//自定义View显示
LYBMBProgressHud.sharedManager().showProgresshudText(withNOCustomView: view, title: "自定义View", detailtitle: "", isOnDismissbg: false, hideAfterdelay: true)
//系统的带动画显示
LYBMBProgressHud.sharedManager().showProgresshudAnimal(with: view, title: "动画", detailtitle: "", isOnDismissbg: false, hideAfterdelay: false)
//3秒钟后消失(GCD的延迟方法)
let additionalTime: DispatchTimeInterval = .seconds(3)
DispatchQueue.main.asyncAfter(deadline: .now() + additionalTime, execute: {
//消失提示
LYBMBProgressHud.sharedManager().dimisAnimalProgress(with: self.view)
})