效果一 只显示文字:
冯绍峰粉色粉色.gif// 只显示文字
- (void)TextButtonAction{
self.HUD = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:_HUD];
_HUD.labelText = @"加载中,请稍等...";
_HUD.mode = MBProgressHUDModeText;
[_HUD showAnimated:YES whileExecutingBlock:^{
sleep(2);
}
completionBlock:^{
[_HUD removeFromSuperview];
_HUD = nil;
}];
}
</br>
效果二 圆形进度条
// 圆形进度条
- (void)CircularButtonAction{
//圆形进度条
self.HUD = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:_HUD];
_HUD.mode = MBProgressHUDModeDeterminate;
_HUD.delegate = self;
_HUD.labelText = @"等待中";
[_HUD showWhileExecuting:@selector(ProgressBar) onTarget:self withObject:nil animated:YES];
}
// 进度条 计算
- (void)ProgressBar{
// 进度指示器,从0.0到1.0,默认值为0.0
float progress = 0.0f;
while (progress < 1.0f) {
progress += 0.01f;
_HUD.progress = progress;
// usleep函数能把进程挂起一段时间, 单位是微秒(千分之一毫秒)
usleep(50000);
}
}
</br>
效果三 文字 加 菊花
// 通常情况 文字 加 菊花
- (void)GeneralButtonAction{
//初始化进度框,置于当前的View当中
self.HUD = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:_HUD];
//如果设置此属性则当前的view置于后台
_HUD.dimBackground = YES;
//设置对话框文字
_HUD.labelText = @"加载中";
//细节文字
_HUD.detailsLabelText = @"请耐心等待";
//显示对话框
[_HUD showAnimated:YES whileExecutingBlock:^{
//对话框显示时需要执行的操作
sleep(3);
}// 在HUD被隐藏后的回调
completionBlock:^{
//操作执行完后取消对话框
[_HUD removeFromSuperview];
_HUD = nil;
}];
}
</br>
效果四 自定义动图
// 自定义 动图
- (void)CustomButtonAction{
//自定义view
self.HUD = [[MBProgressHUD alloc] initWithView:self.view];
//取消背景框
self.HUD.color = [UIColor whiteColor];
[self.view addSubview:_HUD];
UIImageView *images = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 200, 300)];
NSMutableArray *imageArray = [[NSMutableArray alloc]init];
for(int i = 1; i < 8 ; i++){
NSString *imgName = [NSString stringWithFormat:@"%d.tiff",i];
[imageArray addObject:[UIImage imageNamed:imgName]];
}
images.animationDuration = 0.7;
images.animationImages = imageArray;
// 开始播放
[images startAnimating];
//自定义
_HUD.mode = MBProgressHUDModeCustomView;
_HUD.delegate = self;
_HUD.customView = images;
[_HUD show:YES];
//延迟
[_HUD hide:YES afterDelay:2];
}