接下来是整个Demo的完整界面,这里我只选择出了几个常用的对话框,其他样式的在源码提供的Demo里可以找到,要用的话直接参考就可以。
接下来直接上代码了,头文件部分:
- #import <UIKit/UIKit.h>
- #import "MBProgressHUD.h"
- @interface ViewController : UIViewController
- {
- //HUD(Head-Up Display,意思是抬头显示的意思)
- MBProgressHUD *HUD;
- }
- - (IBAction)showTextDialog:(id)sender;
- - (IBAction)showProgressDialog:(id)sender;
- - (IBAction)showProgressDialog2:(id)sender;
- - (IBAction)showCustomDialog:(id)sender;
- - (IBAction)showAllTextDialog:(id)sender;
- @end
实现文件(按钮实现部分):
- - (IBAction)showTextDialog:(id)sender {
- //初始化进度框,置于当前的View当中
- HUD = [[MBProgressHUD alloc] initWithView:self.view];
- [self.view addSubview:HUD];
- //如果设置此属性则当前的view置于后台
- HUD.dimBackground = YES;
- //设置对话框文字
- HUD.labelText = @"请稍等";
- //显示对话框
- [HUD showAnimated:YES whileExecutingBlock:^{
- //对话框显示时需要执行的操作
- sleep(3);
- } completionBlock:^{
- //操作执行完后取消对话框
- [HUD removeFromSuperview];
- [HUD release];
- HUD = nil;
- }];
- }
- - (IBAction)showProgressDialog:(id)sender {
- HUD = [[MBProgressHUD alloc] initWithView:self.view];
- [self.view addSubview:HUD];
- HUD.labelText = @"正在加载";
- //设置模式为进度框形的
- HUD.mode = MBProgressHUDModeDeterminate;
- [HUD showAnimated:YES whileExecutingBlock:^{
- float progress = 0.0f;
- while (progress < 1.0f) {
- progress += 0.01f;
- HUD.progress = progress;
- usleep(50000);
- }
- } completionBlock:^{
- [HUD removeFromSuperview];
- [HUD release];
- HUD = nil;
- }];
- }
- - (IBAction)showProgressDialog2:(id)sender {
- HUD = [[MBProgressHUD alloc] initWithView:self.view];
- [self.view addSubview:HUD];
- HUD.labelText = @"正在加载";
- HUD.mode = MBProgressHUDModeAnnularDeterminate;
- [HUD showAnimated:YES whileExecutingBlock:^{
- float progress = 0.0f;
- while (progress < 1.0f) {
- progress += 0.01f;
- HUD.progress = progress;
- usleep(50000);
- }
- } 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;
- //指定距离中心点的X轴和Y轴的偏移量,如果不指定则在屏幕中间显示
- // HUD.yOffset = 150.0f;
- // HUD.xOffset = 100.0f;
- [HUD showAnimated:YES whileExecutingBlock:^{
- sleep(2);
- } completionBlock:^{
- [HUD removeFromSuperview];
- [HUD release];
- HUD = nil;
- }];
- }
-
// 自定义提示框
_hud = [[MBProgressHUDalloc]initWithView:self.view];
[self.viewaddSubview:_hud];
_hud.labelText =@"操作成功";
_hud.mode =MBProgressHUDModeCustomView;
UIView * view = [[UIViewalloc]initWithFrame:CGRectMake(100,100,100,100)];
view.center = _hud.center;
UIActivityIndicatorView * activity = [[UIActivityIndicatorViewalloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
activity.color = [UIColorwhiteColor];
activity.frame = CGRectMake((view.frame.size.width-50)/2,0,50,50);
// [self.view addSubview:activity];
[view addSubview:activity];
UIImageView * imageView = [[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"zhanghu_touxiang"]];
imageView.frame = CGRectMake((view.frame.size.width-50)/2,Relative_y(activity),50,50);
[view addSubview:imageView];
_hud.customView = view;
[_hudshowAnimated:YESwhileExecutingBlock:^{
[activity startAnimating];
sleep(2);
} completionBlock:^{
[_hud removeFromSuperview];
_hud = nil;
}];
}
依次实现的效果如下:
下面这个效果就类似Android中的Toast:
/*
使用MBProegressHUD注意:
1.mode 默认的显示模式 是MBProgressHUDModeIndeterminate菊花; MBProgressHUDModeDeterminate圆形进度条(最后变为实心圆形);MBProgressHUDModeAnnularDeterminate圆形进度条(空心);MBProgressHUDModeCustomView自定义视图;MBProgressHUDModeText纯文本模式
2.labelText
3.detailLabelText
4.dimBackground 设置此属性则当前的view置于后台
5.removeFromSuperViewOnHide
6.[_hud showAnimated:YES whileExecutingBlock:^{ } completionBlock:^{
}];
7._hud.frame = CGRectMake(0,0,.self.view.frame.size.width ,self.view.frame.size.height);
8.//指定距离中心点的X轴和Y轴的偏移量,如果不指定则在屏幕中间显示
_hud.xOffset 以中心点为起点开始偏移
_hud.yOffset
*/
当你在开发中遇到问题时,最好的解决办法就是冷静思考,一层一层摸索问题的根源,希望自己能在这里继续坚持下去!加油!