MBProgressHUD的封装

1.创建一个MBProgressHUD的Category分类

屏幕快照 2019-11-28 下午1.45.12.png

2.在.h中直接写入

#import <MBProgressHUD/MBProgressHUD.h>

NS_ASSUME_NONNULL_BEGIN

@interface MBProgressHUD (LC)
#pragma mark - 显示在window

//window显示文字
+ (void)showInWindowMessage:(NSString *)message;

//window显示文字延时
+ (void)showInWindowMessage:(NSString *)message delayTime:(NSInteger)time;

//window加载
+ (void)showInWindowActivityWithMessage:(NSString *)message;

//window加载延时
+ (void)showInWindowActivityWithMessage:(NSString *)message delayTime:(NSInteger)time;

//window自定义图片
+ (void)showInWindowCustomImage:(NSString *)imageName message:(NSString *)message;

#pragma mark - 显示在view
//view显示文字
+ (void)showInViewMessage:(NSString *)message;

//view显示文字延时
+ (void)showInViewMessage:(NSString *)message delayTime:(NSInteger)time;

//view加载
+ (void)showInViewActivityWithMessage:(NSString *)message;

//view加载延时
+ (void)showInViewActivityWithMessage:(NSString *)message delayTime:(NSInteger)time;

//view自定义图片
+ (void)showInViewCustomImage:(NSString *)imageName message:(NSString *)message;

#pragma mark - 操作结果提示
//成功提示
+ (void)showSuccessMessage:(NSString *)message;

//失败提示
+ (void)showFailMessage:(NSString *)message;

//警告提示
+ (void)showWarnMessage:(NSString *)message;

//信息提示
+ (void)showInfoMessage:(NSString *)message;

//显示进度条
+ (void)showProgressHudWithMessage:(NSString *)message;

//显示进度条网络请求
+ (void)showHUD;
#pragma mark - 隐藏
//隐藏
+ (void)hideHUD;

@end

3.在.m中直接写入

#import "MBProgressHUD+LC.h"
#import "AppDelegate.h"

//设置RGB颜色
#define ZKRGBColor(r, g, b) [UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1.0]//用10进制表示颜色,例如(255,255,255)黑色

#define SCREEN_WIDTH  [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

@implementation MBProgressHUD (LC)
//window显示文字
+ (void)showInWindowMessage:(NSString *)message {
    [self showMessage:message delayTime:1.0 isWindow:YES];
}

//window显示文字延时
+ (void)showInWindowMessage:(NSString *)message delayTime:(NSInteger)time {
    [self showMessage:message delayTime:time isWindow:YES];
}

//window加载
+ (void)showInWindowActivityWithMessage:(NSString *)message {
    [self showActivityMessage:message isWindow:YES delayTime:1.0];
}

//window加载延时
+ (void)showInWindowActivityWithMessage:(NSString *)message delayTime:(NSInteger)time {
    [self showActivityMessage:message isWindow:YES delayTime:time];
}

//window自定义图片
+ (void)showInWindowCustomImage:(NSString *)imageName message:(NSString *)message {
    [self showCustomImage:imageName message:message isWindow:YES];
}

#pragma mark - 显示在view
//view显示文字
+ (void)showInViewMessage:(NSString *)message {
    [self showMessage:message delayTime:1.0 isWindow:NO];
}

//view显示文字延时
+ (void)showInViewMessage:(NSString *)message delayTime:(NSInteger)time {
    [self showMessage:message delayTime:time isWindow:NO];
}

//view加载
+ (void)showInViewActivityWithMessage:(NSString *)message {
    [self showActivityMessage:message isWindow:NO delayTime:1.0];
}

//view加载延时
+ (void)showInViewActivityWithMessage:(NSString *)message delayTime:(NSInteger)time {
    [self showActivityMessage:message isWindow:NO delayTime:time];
}

//view自定义图片
+ (void)showInViewCustomImage:(NSString *)imageName message:(NSString *)message {
    [self showCustomImage:imageName message:message isWindow:NO];
}

#pragma mark - 操作结果提示
//成功提示
+ (void)showSuccessMessage:(NSString *)message {
    [self showCustomImage:@"MBHUD_Success" message:message];
}

//失败提示
+ (void)showFailMessage:(NSString *)message {
    [self showCustomImage:@"MBHUD_Error" message:message];
}

//警告提示
+ (void)showWarnMessage:(NSString *)message {
    [self showCustomImage:@"MBHUD_Warn" message:message];
}

//信息提示
+ (void)showInfoMessage:(NSString *)message {
    [self showCustomImage:@"MBHUD_Info" message:message];
}

//默认显示在window
+ (void)showCustomImage:(NSString *)imageName message:(NSString *)message {
    [self showCustomImage:imageName message:message isWindow:YES];
}

+ (void)showProgressHudWithMessage:(NSString *)message {
    MBProgressHUD *hud = [self createMBProgerssHUDWithMessage:message isWindow:YES];
    hud.mode = MBProgressHUDModeIndeterminate;
    hud.bezelView.color = [UIColor  blackColor];
    hud.contentColor = [UIColor whiteColor];
}
//显示进度条网络请求
+ (void)showHUD{
    MBProgressHUD *hud = [self createMBProgerssHUDWithMessage:@"正在请求请稍后" isWindow:YES];
    hud.mode = MBProgressHUDModeIndeterminate;
    hud.bezelView.color = [UIColor  blackColor];
    hud.contentColor = [UIColor whiteColor];
}
#pragma mark - 隐藏
//隐藏
+ (void)hideHUD {
    UIView *windowView = (UIView *)[UIApplication sharedApplication].delegate.window;
    [self hideHUDForView:windowView animated:YES];
    [self hideHUDForView:[self currentUIViewController].view animated:YES];
}

#pragma mark - Private
/**
 加载动态图片
 */
+ (void)showActivityMessage:(NSString*)message
                   isWindow:(BOOL)isWindow
                  delayTime:(NSInteger)delayTime {
    
    MBProgressHUD *hud  = [self createMBProgerssHUDWithMessage:message isWindow:isWindow];
    hud.mode = MBProgressHUDModeIndeterminate;
    hud.activityIndicatorColor = [UIColor whiteColor];
    hud.label.textColor=[UIColor whiteColor];
    hud.bezelView.color = [UIColor  blackColor];
    hud.square = YES;
    if (delayTime > 0) {
        [hud hideAnimated:YES afterDelay:delayTime];
    }
  
}

/**
 自定义图片
 */
+ (void)showCustomImage:(NSString *)imageName
                message:(NSString *)message
               isWindow:(BOOL)isWindow {
    
    MBProgressHUD *hud = [self createMBProgerssHUDWithMessage:message isWindow:isWindow];
    hud.mode = MBProgressHUDModeCustomView;
    hud.square = YES;
    hud.contentColor = [UIColor whiteColor];
    hud.bezelView.backgroundColor = ZKRGBColor(30, 30, 30);
    hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
    hud.animationType = MBProgressHUDAnimationZoomOut;
    [hud hideAnimated:YES afterDelay:0.7];
}

/**
 显示信息延时
 */
+ (void)showMessage:(NSString *)message
          delayTime:(NSInteger)delayTime
           isWindow:(BOOL)isWindow {
    
    MBProgressHUD *hud = [self createMBProgerssHUDWithMessage:nil isWindow:isWindow];
    hud.detailsLabel.text=message;
    hud.mode = MBProgressHUDModeText;
    hud.detailsLabel.textColor = [UIColor whiteColor];
    hud.bezelView.backgroundColor =ZKRGBColor(30, 30, 30);
    hud.detailsLabel.font = [UIFont systemFontOfSize:15.0f];
    //设置大小
    hud.minSize = CGSizeMake(200,50);
    [hud hideAnimated:YES afterDelay:delayTime];
}

/**
 显示信息
 */
+ (MBProgressHUD *)createMBProgerssHUDWithMessage:(NSString *)message isWindow:(BOOL)isWindow {
    
    UIView *view = isWindow ? (UIView *)[UIApplication sharedApplication].delegate.window : [self currentUIViewController].view;
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.label.textColor = ZKRGBColor(255, 255, 255);
    hud.label.font = [UIFont systemFontOfSize:12.0f];
    hud.label.text = message;
    hud.removeFromSuperViewOnHide = YES;
    return hud;
}

//获取屏幕当前显示的ViewController
+ (UIViewController *)currentUIViewController {
    UIViewController *superViewController = [[self class] currentWindowViewController];
    if ([superViewController isKindOfClass:[UITabBarController class]]) {
        
        UIViewController *tabSelectVC = ((UITabBarController *)superViewController).selectedViewController;
        if ([tabSelectVC isKindOfClass:[UINavigationController class]]) {
            return ((UINavigationController *)tabSelectVC).viewControllers.lastObject;
        }
        
    }else if ([superViewController isKindOfClass:[UINavigationController class]]) {
        return ((UINavigationController *)superViewController).viewControllers.lastObject;
    }
    return superViewController;
}

+ (UIViewController *)currentWindowViewController {
    UIViewController *viewController = nil;
    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
    if (appDelegate.window.windowLevel != UIWindowLevelNormal) {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for (UIWindow *tempWindow in windows) {
            if (tempWindow.windowLevel == UIWindowLevelNormal) {
                appDelegate.window = tempWindow;
                break;
            }
        }
    }
    UIView *frontView = [[appDelegate.window subviews]objectAtIndex:0];
    id nextResponder = [frontView nextResponder];
    if ([nextResponder isKindOfClass:[UIViewController class]]) {
        viewController = nextResponder;
    }else {
        viewController = appDelegate.window.rootViewController;
    }
    return viewController;
}

4.圆形进度条

- (void)CircularButtonAction{

    self.HUD  =  [[MBProgressHUD alloc]initWithView:self.view];
    [self.view addSubview:self.HUD];
    self.HUD.tag=1000;
    self.HUD.mode = MBProgressHUDModeDeterminateHorizontalBar;
    self.HUD.label.text = @"Downloading...";
    self.HUD.square = YES;
    self.HUD.contentColor=[UIColor redColor];
    [self.HUD showAnimated:YES];

    self.updatePerTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self
                                                         selector:@selector(updateTrancodePercentage:) userInfo:nil repeats:YES];
}
- (void)updateTrancodePercentage:(NSTimer *)theTimer {
    if (self.HUD != nil) {
        static  float i = 0;
        i++;
        self.HUD.progress = i/100;
        if (i<20) {
            self.HUD.label.text = [NSString stringWithFormat:@"当前进度%.0f%%",i];
        }else{
            [self.HUD hideAnimated:YES];
        }
    }
}

5 自定义动图

- (void)CustomButtonAction{
    //自定义view
    MBProgressHUD*HUD = [[MBProgressHUD alloc] initWithView:self.view];
    //取消背景框
    HUD.bezelView.color = [UIColor whiteColor];
    [self.view addSubview:HUD];

    UIImageView *images = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 200, 300)];
    NSMutableArray *imageArray = [[NSMutableArray alloc]init];
    for(int i = 1; i < 4 ; i++){
        [imageArray addObject:[UIImage imageNamed:[NSString stringWithFormat:@"%d.ty",i]]];
    }
    images.animationImages = imageArray;
    images.animationDuration = 0.7;
    // 开始播放
    [images startAnimating];
    //自定义
    HUD.mode = MBProgressHUDModeCustomView;
    HUD.delegate = self;
    HUD.customView = images;
    [HUD showAnimated:YES];
    //延迟
    [HUD hideAnimated:YES afterDelay:2];

}

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,826评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,968评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,234评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,562评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,611评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,482评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,271评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,166评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,608评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,814评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,926评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,644评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,249评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,866评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,991评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,063评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,871评论 2 354

推荐阅读更多精彩内容

  • 1、前言 在ios开发中,最经典也是最常用的提示框就是MBProgressHUD了,用于在执行一些任务时的提示效果...
    MrFire_阅读 13,934评论 22 64
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,101评论 1 32
  • 这里有源码分析://www.greatytc.com/p/485b8d75ccd4 源码笔记---MBPr...
    晓飞90阅读 824评论 0 2
  • 说明:这篇文章主要是自己开发的时候查阅使用,参考了许多的资料。 参考源码:https://github.com/T...
    晓飞90阅读 404评论 0 0
  • 每个人都喜欢钱,凡是道貌岸然说不喜欢的都是虚伪的;所有人也都缺钱,只不过缺的量级和程度不一样。 看,开篇两句估计就...
    尘世知行者阅读 189评论 1 0