mbprogresshud的二次封装

mbprogresshud 是一款非常方便hud视图 称为弹框 虽然方便 但是我们开发者实在不能容忍每次写一个弹框 都要写那么多代码 于是我觉得 来一份mbprogresshud的小小的封装

新建一个nsobject的类

.h 这样 一个属性hud的属性 2个实例方法 和一些类方法


@property(nonatomic,strong)MBProgressHUD*hud;

-(void)starProgresshud;

-(void)stopProgresshud;

// 加载弹框

+(void)ShowDoProgressAfterTime:(NSTimeInterval)time;

// 默认文本弹框

+(void)showNomorText:(NSString*)text AfterTime:(NSTimeInterval)time;

// 文本显示弹框(黑色)

+(void)showText:(NSString*)text AfterTime:(NSTimeInterval)time;

// 圆环形状弹框

+(void)showRotundityAfterTime:(NSTimeInterval)time;

// 错误信息提示弹框

+(void)showError:(NSString*)text AfterTime:(NSTimeInterval)time errorimage:(UIImage*)image;

// 正确信息提示

+(void)showSuccess:(NSString*)text AfterTime:(NSTimeInterval)time successimage:(UIImage*)image;

实例方法主要是针对于一种不知道确定弹框消失时间的 例如网络请求完成之后 需要把加载的hud取消掉 可以使用实例方法加载 然后 网络请求成功的回调里面取消hud

类方法 主要是针对几块


// 加载弹框

// 默认文本弹框

// 文本显示弹框(黑色)

// 圆环形状弹框

// 错误信息提示弹框

// 正确信息提示

具体实现 我们来看.m

首先 我们来看重写父类方法 和实例方法


-(instancetype)init

{

if(self= [superinit]) {

}

returnself;

}

// 开始加载动画

-(void)starProgresshud

{

UIWindow*window = [[UIApplicationsharedApplication].windowslastObject];

self.hud= [MBProgressHUDshowHUDAddedTo:windowanimated:YES];

}

// 结束加载

-(void)stopProgresshud

{

if(self.hud) {

[self.hudhideAnimated:YES];

}else

{

}

}

为了避免代码重复 几个类方法中 也会有重复的代码 我们坚定写一个创建的私有方法


// 创建hud

+(MBProgressHUD*)createHud

{

UIWindow*window = [[UIApplicationsharedApplication].windowslastObject];

MBProgressHUD*hud = [MBProgressHUDshowHUDAddedTo:windowanimated:YES];

returnhud;

}

具体方法实现 需要那些参数 就不一一细说了 很简单的一次二次封装 但是很实用 至少我们写弹框的时候 可以避免写大量重复的代码


// 默认文本样式

+(void)showNomorText:(NSString*)text AfterTime:(NSTimeInterval)time;

{

MBProgressHUD*hud = [selfcreateHud];

hud.mode=MBProgressHUDModeText;

hud.label.text= text;

[hudhideAnimated:YESafterDelay:time];

}

// 黑色文本样文本样式

+(void)showText:(NSString*)text AfterTime:(NSTimeInterval)time

{

MBProgressHUD*hud = [selfcreateHud];

hud.mode=MBProgressHUDModeText;

hud.label.text= text;

hud.bezelView.backgroundColor= [UIColorblackColor];

hud.label.textColor= [UIColorwhiteColor];

[hudhideAnimated:YESafterDelay:time];

}

// 圆环

+(void)showRotundityAfterTime:(NSTimeInterval)time

{

MBProgressHUD*hud = [selfcreateHud];

hud.mode=MBProgressHUDModeDeterminate;

[hudhideAnimated:YESafterDelay:time];

}

// 错误信息提示弹框

+(void)showError:(NSString*)text AfterTime:(NSTimeInterval)time errorimage:(UIImage*)image;

{

MBProgressHUD*hud = [selfcreateHud];

hud.mode=MBProgressHUDModeCustomView;

UIImageView*imageV=[[UIImageViewalloc]initWithImage:image];

hud.customView= imageV;

hud.square=YES;

hud.label.text=[NSStringstringWithFormat:@"%@",text];

[hudhideAnimated:YESafterDelay:time];

}

// 正确信息提示

+(void)showSuccess:(NSString*)text AfterTime:(NSTimeInterval)time successimage:(UIImage*)image;

{

MBProgressHUD*hud = [selfcreateHud];

hud.mode=MBProgressHUDModeCustomView;

hud.customView=[[UIImageViewalloc]initWithImage:image];

hud.label.text=[NSStringstringWithFormat:@"%@",text];

[hudhideAnimated:YESafterDelay:time];

}

你可能感兴趣的:(mbprogresshud的二次封装)