下面是我基于MBProgressHUD的封装
@interface MBHUDHelper : NSObject
/**
* 显示MBProgressHUD指示器
* api parameters 说明
* aTitle 标题
* aMsg 信息
* aImg 图片, 为nil时,只显示标题
* d 延时消失时间, 为0时需要主动隐藏
* blockE 执行的代码快
* blockF 结束时的代码块
* 执行时改变hub需要调用Common_MainFun(aFun)
*/
+ (void)hiddenMBProgressHUD;
+ (MBProgressHUD *)MBProgressHUD;
+ (MBProgressHUD *)showMBProgressHUDTitle:(NSString *)aTitle
msg:(NSString *)aMsg
image:(UIImage *)aImg
dimBG:(BOOL)dimBG
delay:(float)d;
+ (MBProgressHUD *)showMBProgressHUDModeIndeterminateTitle:(NSString *)aTitle
msg:(NSString *)aMsg
dimBG:(BOOL)dimBG
delay:(float)d;
+ (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
@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