MBProgressHUD的简单封装

本文介绍了一个基于MBProgressHUD的封装实现,提供了多种MBProgressHUD展示方式,包括文本、进度条、取消按钮等,并支持延迟隐藏及自定义代码块执行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下面是我基于MBProgressHUD的封装

#import <Foundation/Foundation.h>
#import "MBProgressHUD.h"

@interface MBHUDHelper : NSObject


/**
 * 显示MBProgressHUD指示器
 * api parameters 说明
 * aTitle 标题
 * aMsg 信息
 * aImg 图片, 为nil时,只显示标题
 * d 延时消失时间, 为0时需要主动隐藏
 * blockE 执行的代码快
 * blockF 结束时的代码块
 * 执行时改变hub需要调用Common_MainFun(aFun)
 */
#define HIDDENMBProgressHUD [MBHUDHelper hiddenMBProgressHUD];
+ (void)hiddenMBProgressHUD;

+ (MBProgressHUD *)MBProgressHUD;

#define SHOWMBProgressHUD(aTitle, aMsg, aImg, aDimBG, aDelay) [MBHUDHelper showMBProgressHUDTitle:aTitle msg:aMsg image:aImg dimBG:aDimBG delay:aDelay];
+ (MBProgressHUD *)showMBProgressHUDTitle:(NSString *)aTitle
                                      msg:(NSString *)aMsg
                                    image:(UIImage *)aImg
                                    dimBG:(BOOL)dimBG
                                    delay:(float)d;

#define SHOWMBProgressHUDIndeterminate(aTitle, aMsg, aDimBG ,aDelay) [MBHUDHelper showMBProgressHUDModeIndeterminateTitle:aTitle msg:aMsg dimBG:aDimBG delay:aDelay];
+ (MBProgressHUD *)showMBProgressHUDModeIndeterminateTitle:(NSString *)aTitle
                                                       msg:(NSString *)aMsg
                                                     dimBG:(BOOL)dimBG
                                                      delay:(float)d;




#define SHOWMBProgressHUDCancelIndeterminate(aTitle, aMsg, aDimBG ,aDelay) [MBHUDHelper SHOWMBProgressHUDCancelIndeterminate:aTitle msg:aMsg dimBG:aDimBG delay:aDelay];
+ (MBProgressHUD *)SHOWMBProgressHUDCancelIndeterminate:(NSString *)aTitle
                                                       msg:(NSString *)aMsg
                                                     dimBG:(BOOL)dimBG
                                                     delay:(float)d;

+ (MBProgressHUD *)showMBProgressHUDTitle:(NSString *)aTitle
                                      msg:(NSString *)aMsg
                                    dimBG:(BOOL)dimBG
                             executeBlock:(void(^)(MBProgressHUD *hud))blockE
                              finishBlock:(void(^)(void))blockF;
+ (MBProgressHUD *)showMessag:(NSString *)message;

@end
#import "MBHUDHelper.h"


@implementation MBHUDHelper

static MBProgressHUD *HUD = nil;

+ (void)hiddenMBProgressHUD
{
    [HUD hide:YES];

    DownLoadManager.cancelView.hidden=YES;
}

+ (MBProgressHUD *)MBProgressHUD
{
    return HUD;
}

+ (MBProgressHUD *)showMBProgressHUDTitle:(NSString *)aTitle
                                      msg:(NSString *)aMsg
                                    image:(UIImage *)aImg
                                    dimBG:(BOOL)dimBG
                                    delay:(float)d
{
    UIViewController *vc = [self topMostController];

    if (vc == nil)
    {
        return nil;
    }

    if (nil == HUD)
    {
        HUD = [[MBProgressHUD alloc] initWithView:vc.view];
    }

    [vc.view addSubview:HUD];


    if (aTitle || aMsg)
    {
        HUD.mode = MBProgressHUDModeText;
        HUD.labelText = aTitle;
        HUD.detailsLabelText = aMsg;
    }

    if (aImg)
    {
        UIImageView *img = [[UIImageView alloc] initWithImage:aImg];
        HUD.customView = img;
        HUD.mode = MBProgressHUDModeCustomView;
    }

    HUD.removeFromSuperViewOnHide = YES;
    HUD.dimBackground = NO;
    HUD.userInteractionEnabled = !dimBG;
    [HUD show:YES];

    if (d > 0)
    {
        [HUD hide:YES afterDelay:d];
    }

    return HUD;
}

+ (MBProgressHUD *)showMBProgressHUDModeIndeterminateTitle:(NSString *)aTitle
                                                       msg:(NSString *)aMsg
                                                     dimBG:(BOOL)dimBG
                                                      delay:(float)d
{
    UIViewController *vc = [self topMostController];

    if (vc == nil)
    {
        return nil;
    }

    if (nil == HUD)
    {
        HUD = [[MBProgressHUD alloc] initWithView:vc.view];
    }

    [vc.view addSubview:HUD];


    HUD.mode = MBProgressHUDModeIndeterminate;
    HUD.labelText = aTitle;
    HUD.detailsLabelText = aMsg;
    HUD.removeFromSuperViewOnHide = YES;
    HUD.dimBackground = dimBG;
    [HUD show:YES];
    if (d > 0)
    {
        [HUD hide:YES afterDelay:d];
    }
    return HUD;
}


+ (MBProgressHUD *)SHOWMBProgressHUDCancelIndeterminate:(NSString *)aTitle
                                                    msg:(NSString *)aMsg
                                                  dimBG:(BOOL)dimBG
                                                  delay:(float)d{
    UIViewController *vc = [self topMostController];

    if (vc == nil)
    {
        return nil;
    }

    if (nil == HUD)
    {
        HUD = [[MBProgressHUD alloc] initWithView:vc.view];
    }

    [vc.view addSubview:HUD];


    if (nil == DownLoadManager.cancelView){
        DownLoadManager.cancelView=[[CancelView alloc] initWithFrame:CGRectMake(kDeviceWidth-80, 25, 80, 35)];

    }
    DownLoadManager.cancelView.hidden=NO;
    [vc.view addSubview:DownLoadManager.cancelView];

    HUD.mode = MBProgressHUDModeIndeterminate;
    HUD.labelText = aTitle;
    HUD.detailsLabelText = aMsg;
    HUD.removeFromSuperViewOnHide =NO;
    HUD.dimBackground = dimBG;
    [HUD show:YES];
    if (d > 0)
    {
        [HUD hide:YES afterDelay:d];
    }
    return HUD;
}



+ (MBProgressHUD *)showMBProgressHUDTitle:(NSString *)aTitle
                                      msg:(NSString *)aMsg
                                    dimBG:(BOOL)dimBG
                             executeBlock:(void(^)(MBProgressHUD *hud))blockE
                              finishBlock:(void(^)(void))blockF
{
    UIViewController *vc = [self topMostController];

    if (vc == nil)
    {
        return nil;
    }

    if (nil == HUD)
    {
        HUD = [[MBProgressHUD alloc] initWithView:vc.view];
    }

    [vc.view addSubview:HUD];

    HUD.labelText = aTitle;
    HUD.detailsLabelText = aMsg;
    HUD.removeFromSuperViewOnHide = YES;
    HUD.dimBackground = dimBG;

    [HUD showAnimated:YES whileExecutingBlock:^{
        blockE(HUD);
    } completionBlock:^{
        //[hud hide:YES];
        blockF();
    }];

    return HUD;
}
+ (MBProgressHUD *)showMessag:(NSString *)message 
{
    UIViewController *vc = [self topMostController];

    if (vc == nil)
    {
        return nil;
    }

    if (nil == HUD)
    {
        HUD = [[MBProgressHUD alloc] initWithView:vc.view];
    }

    [vc.view addSubview:HUD];


    HUD.detailsLabelText = message;
    HUD.detailsLabelFont = [UIFont systemFontOfSize:15];
    HUD.mode = MBProgressHUDModeText;
    HUD.removeFromSuperViewOnHide = YES;
    HUD.margin = 10.f;
    //hud.lineBreakMode = UILineBreakModeWordWrap;

    [HUD show:YES];
    [HUD hide:YES afterDelay:1.0f];
    return HUD;
}

@end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值