转自:http://www.cnblogs.com/iOS-dd/p/3416408.html
MBProgressHUD是一个开源类库,实现了各种样式的提示框, 下载地址:https://github.com/jdg/MBProgressHUD,然后把两个MBProgressHUD.h和MBProgressHUD.m放到自己的项目就可以了。这里有一个小Demo可以参考一下。
头文件部分:
#import <UIKit/UIKit.h>
#import "MBProgressHUD.h"
@interface ViewController : UIViewController
{
MBProgressHUD *HUD;
}
- (IBAction)showTextDialog:(id)sender; //文本提示框,默认情况下
- (IBAction)showProgressOne:(id)sender; //第一种加载提示框
- (IBAction)showProgressTwo:(id)sender; //第二种加载提示框
- (IBAction)showProgressThree:(id)sender; //第三种加载提示框
- (IBAction)showCustomDialog:(id)sender; //自定义提示框,显示打钩效果
- (IBAction)showAllTextDialog:(id)sender; //显示纯文本提示框
@end
实现文件部分
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)showTextDialog:(id)sender {
HUD = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:HUD];
HUD.dimBackground = YES; //把当前的view置于后台
HUD.labelText = @"请稍等";
//显示对话框
[HUD showAnimated:YES whileExecutingBlock:^{
sleep(3);
} completionBlock:^{
}];
}
- (IBAction)showProgressOne:(id)sender {
HUD = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:HUD];
HUD.labelText = @"正在加载";
HUD.mode = MBProgressHUDModeDeterminate;
//HUD.mode = MBProgressHUDModeAnnularDeterminate;
[HUD showAnimated:YES whileExecutingBlock:^{
float progress = 0.0f;
while (progress < 1.0f) {
progress += 0.01f;
HUD.progress = progress;
usleep(5000);
}
} completionBlock:^{
[HUD removeFromSuperview];
[HUD release];
HUD = nil;
}];
}
- (IBAction)showProgressTwo:(id)sender {
HUD = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:HUD];
HUD.labelText = @"正在加载";
HUD.mode = HUD.mode = MBProgressHUDModeAnnularDeterminate;
[HUD showAnimated:YES whileExecutingBlock:^{
float progress = 0.0f;
while (progress < 1.0f) {
progress += 0.01f;
HUD.progress = progress;
usleep(5000);
}
} completionBlock:^{
[HUD removeFromSuperview];
[HUD release];
HUD = nil;
}];
}
- (IBAction)showProgressThree:(id)sender {
HUD = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:HUD];
HUD.labelText = @"正在加载";
HUD.mode = MBProgressHUDModeDeterminateHorizontalBar;
[HUD showAnimated:YES whileExecutingBlock:^{
float progress = 0.0f;
while (progress < 1.0f) {
progress += 0.01f;
HUD.progress = progress;
usleep(5000);
}
} completionBlock:^{
[HUD removeFromSuperview];
[HUD release];
HUD = nil;
}];
}
- (IBAction)showCustomDialog:(id)sender {
HUD = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:HUD];
HUD.labelText = @"操作成功";
HUD.mode = MBProgressHUDModeCustomView;
HUD.customView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Checkmark"]] autorelease];
[HUD showAnimated:YES whileExecutingBlock:^{
sleep(2);
} completionBlock:^{
[HUD removeFromSuperview];
[HUD release];
HUD = nil;
}];
}
- (IBAction)showAllTextDialog:(id)sender {
HUD = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:HUD];
HUD.labelText = @"操作成功";
HUD.mode = MBProgressHUDModeText;
HUD.customView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Checkmark"]] autorelease];
[HUD showAnimated:YES whileExecutingBlock:^{
sleep(2);
} completionBlock:^{
[HUD removeFromSuperview];
[HUD release];
HUD = nil;
}];
}
- (void)dealloc {
[super dealloc];
}
@end
实现效果如图所示:
1、默认效果,也就是MBProgressHUDModeIndeterminate
2、第一种加载提示框,MBProgressHUDModeDeterminate
3、第二种加载提示MBProgressHUDModeAnnularDeterminate
4、第三种加载提示框,MBProgressHUDModeDeterminateHorizontalBar
5、自定义提示框 ,可以带图片的MBProgressHUDModeCustomView
6.纯文本提示框