下载地址:https://github.com/jdg/MBProgressHUD/
//方式1.直接在View上show
HUD = [[MBProgressHUD showHUDAddedTo:self.view animated:YES] retain];
HUD.delegate = self;
//常用的设置
//小矩形的背景色
HUD.color = [UIColor clearColor];//这儿表示无背景
//显示的文字
HUD.labelText = @"Test";
//细节文字
HUD.detailsLabelText = @"Test detail";
//是否有庶罩
HUD.dimBackground = YES;
[HUD hide:YES afterDelay:2];
//只显示文字
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeText;
hud.labelText = @"Some message...";
hud.margin = 10.f;
hud.yOffset = 150.f;
hud.removeFromSuperViewOnHide = YES;
[hud hide:YES afterDelay:3];
//方式2.initWithView
//use block
HUD = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:HUD];
HUD.labelText = @"Test";
[HUD showAnimated:YES whileExecutingBlock:^{
NSLog(@"%@",@"do somethings....");
[self doTask];
} completionBlock:^{
[HUD removeFromSuperview];
[HUD release];
}];
//圆形进度条
HUD = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:HUD];
HUD.mode = MBProgressHUDModeAnnularDeterminate;
HUD.delegate = self;
HUD.labelText = @"Loading";
[HUD showWhileExecuting:@selector(myProgressTask) onTarget:self withObject:nil animated:YES];
//自定义view
HUD = [[MBProgressHUD alloc] initWithView:self.view];
HUD.customView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]] autorelease];
// Set custom view mode
HUD.mode = MBProgressHUDModeCustomView;
HUD.delegate = self;
HUD.labelText = @"Completed";
[HUD show:YES];
[HUD hide:YES afterDelay:3];
代理方法:
#pragma mark -
#pragma mark HUD的代理方法,关闭HUD时执行
-(void)hudWasHidden:(MBProgressHUD *)hud
{
[hud removeFromSuperview];
[hud release];
hud = nil;
}
二个task
-(void) doTask{
//你要进行的一些逻辑操作
sleep(2);
}
-(void) myProgressTask{
float progress = 0.0f;
while (progress < 1.0f) {
progress += 0.01f;
HUD.progress = progress;
usleep(50000);
}
}
注意: 其实,第三方库都是开源的,我们可以在其基础上添加另外自己需要的功能,如果觉得上面显示文字代码太不好用,可以添加一个分类,对它设置文字的方法再次封装如下所示:
添加一个分类:MBProgressHUD+XYQ
MBProgressHUD+XYQ.h文件:
#import "MBProgressHUD" @interface MBProgressHUD (XYQ) + (void)showSuccess:(NSString *)success toView:(UIView *)view; + (void)showError:(NSString *)error toView:(UIView *)view; + (MBProgressHUD *)showMessage:(NSString *)message toView:(UIView *)view; + (void)showSuccess:(NSString *)success; + (void)showError:(NSString *)error; + (MBProgressHUD *)showMessage:(NSString *)message; + (void)hideHUDForView:(UIView *)view; + (void)hideHUD; @end
MBProgressHUD+XYQ.m文件:
#import "MBProgressHUD+XYQ.h" @implementation MBProgressHUD (XYQ) #pragma mark 显示信息 + (void)show:(NSString *)text icon:(NSString *)icon view:(UIView *)view { if (view == nil) view = [[UIApplication sharedApplication].windows lastObject]; // 快速显示一个提示信息 MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES]; hud.labelText = text; // 设置图片 hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"MBProgressHUD.bundle/%@", icon]]]; // 再设置模式 hud.mode = MBProgressHUDModeCustomView; // 隐藏时候从父控件中移除 hud.removeFromSuperViewOnHide = YES; // 1秒之后再消失 [hud hide:YES afterDelay:1.2]; } #pragma mark 显示错误信息 + (void)showError:(NSString *)error toView:(UIView *)view{ [self show:error icon:@"error.png" view:view]; } + (void)showSuccess:(NSString *)success toView:(UIView *)view { [self show:success icon:@"success.png" view:view]; } #pragma mark 显示一些信息 + (MBProgressHUD *)showMessage:(NSString *)message toView:(UIView *)view { if (view == nil) view = [[UIApplication sharedApplication].windows lastObject]; // 快速显示一个提示信息 MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES]; hud.labelText = message; // 隐藏时候从父控件中移除 hud.removeFromSuperViewOnHide = YES; // YES代表需要蒙版效果 hud.dimBackground = YES; return hud; } + (void)showSuccess:(NSString *)success { [self showSuccess:success toView:nil]; } + (void)showError:(NSString *)error { [self showError:error toView:nil]; } + (MBProgressHUD *)showMessage:(NSString *)message { return [self showMessage:message toView:nil]; } + (void)hideHUDForView:(UIView *)view { [self hideHUDForView:view animated:YES]; } + (void)hideHUD { [self hideHUDForView:nil]; } @end
更详细的讲解请看该链接:http://blog.csdn.net/ryantang03/article/details/7877120
程序猿神奇的手,每时每刻,这双手都在改变着世界的交互方式!
本文转自当天真遇到现实博客园博客,原文链接:http://www.cnblogs.com/XYQ-208910/p/5138856.html
,如需转载请自行联系原作者