MBProgressHUD的简单封装

    xiaoxiao2023-03-24  1

    下面是我基于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
    转载请注明原文地址: https://ju.6miu.com/read-1200150.html
    最新回复(0)