MBProgressHUD的使用

来源:互联网 发布:天猫和淘宝质量一样吗 编辑:程序博客网 时间:2024/06/04 20:56
转载地址:http://blog.csdn.net/tangren03/article/details/7877120

有一个属性可以设置MBProgressHUD的显示方式:使MBProgressHUD显示时,可以执行别的动作,不锁定屏幕

即:HUD.userInteractionEnabled =NO;默认该值为YES锁定屏幕的(即显示HUD时界面不响应别的动作)


MBProgressHUD是一个开源项目,实现了很多种样式的提示框,使用上简单、方便,并且可以对显示的内容进行自定义,功能很强大,很多项目中都有使用到。到GitHub上可以下载到项目源码https://github.com/jdg/MBProgressHUD,下载下来后直接把MBProgressHUD.h和MBProgressHUD.m拖入工程中就行,别忘了选择拷贝到工程。完了在需要使用的地方导入头文件就可以开始使用了。首先看下工程截图:

                                                               

接下来是整个Demo的完整界面,这里我只选择出了几个常用的对话框,其他样式的在源码提供的Demo里可以找到,要用的话直接参考就可以。

                                                                      

接下来直接上代码了,头文件部分:

[cpp] view plaincopy
  1. #import <UIKit/UIKit.h> 
  2. #import "MBProgressHUD.h" 
  3.  
  4. @interface ViewController : UIViewController 
  5.    //HUD(Head-Up Display,意思是抬头显示的意思) 
  6.     MBProgressHUD *HUD; 
  7.  
  8. - (IBAction)showTextDialog:(id)sender; 
  9. - (IBAction)showProgressDialog:(id)sender; 
  10. - (IBAction)showProgressDialog2:(id)sender; 
  11. - (IBAction)showCustomDialog:(id)sender; 
  12. - (IBAction)showAllTextDialog:(id)sender; 
  13.  
  14. @end 

实现文件(按钮实现部分):

[cpp] view plaincopy
  1. - (IBAction)showTextDialog:(id)sender { 
  2.    //初始化进度框,置于当前的View当中 
  3.     HUD = [[MBProgressHUD alloc] initWithView:self.view]; 
  4.     [self.view addSubview:HUD]; 
  5.      
  6.    //如果设置此属性则当前的view置于后台 
  7.     HUD.dimBackground = YES; 
  8.      
  9.    //设置对话框文字 
  10.     HUD.labelText = @"请稍等"
  11.      
  12.    //显示对话框 
  13.     [HUD showAnimated:YES whileExecutingBlock:^{ 
  14.        //对话框显示时需要执行的操作 
  15.         sleep(3); 
  16.     } completionBlock:^{ 
  17.        //操作执行完后取消对话框 
  18.         [HUD removeFromSuperview]; 
  19.         [HUD release]; 
  20.         HUD = nil; 
  21.     }]; 
  22.  
  23. - (IBAction)showProgressDialog:(id)sender { 
  24.     HUD = [[MBProgressHUD alloc] initWithView:self.view]; 
  25.     [self.view addSubview:HUD]; 
  26.     HUD.labelText = @"正在加载"
  27.      
  28.    //设置模式为进度框形的 
  29.     HUD.mode = MBProgressHUDModeDeterminate; 
  30.     [HUD showAnimated:YES whileExecutingBlock:^{ 
  31.        float progress = 0.0f; 
  32.        while (progress < 1.0f) { 
  33.             progress += 0.01f; 
  34.             HUD.progress = progress; 
  35.             usleep(50000); 
  36.         } 
  37.     } completionBlock:^{ 
  38.         [HUD removeFromSuperview]; 
  39.         [HUD release]; 
  40.         HUD = nil; 
  41.     }]; 
  42.  
  43. - (IBAction)showProgressDialog2:(id)sender { 
  44.     HUD = [[MBProgressHUD alloc] initWithView:self.view]; 
  45.     [self.view addSubview:HUD]; 
  46.     HUD.labelText = @"正在加载"
  47.     HUD.mode = MBProgressHUDModeAnnularDeterminate; 
  48.      
  49.     [HUD showAnimated:YES whileExecutingBlock:^{ 
  50.        float progress = 0.0f; 
  51.        while (progress < 1.0f) { 
  52.             progress += 0.01f; 
  53.             HUD.progress = progress; 
  54.             usleep(50000); 
  55.         } 
  56.     } completionBlock:^{ 
  57.         [HUD removeFromSuperview]; 
  58.         [HUD release]; 
  59.         HUD = nil; 
  60.     }]; 
  61.  
  62. - (IBAction)showCustomDialog:(id)sender { 
  63.     HUD = [[MBProgressHUD alloc] initWithView:self.view]; 
  64.     [self.view addSubview:HUD]; 
  65.     HUD.labelText = @"操作成功"
  66.     HUD.mode = MBProgressHUDModeCustomView; 
  67.     HUD.customView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Checkmark"]] autorelease]; 
  68.     [HUD showAnimated:YES whileExecutingBlock:^{ 
  69.         sleep(2); 
  70.     } completionBlock:^{ 
  71.         [HUD removeFromSuperview]; 
  72.         [HUD release]; 
  73.         HUD = nil; 
  74.     }]; 
  75.      
  76.  
  77. - (IBAction)showAllTextDialog:(id)sender { 
  78.     HUD = [[MBProgressHUD alloc] initWithView:self.view]; 
  79.     [self.view addSubview:HUD]; 
  80.     HUD.labelText = @"操作成功"
  81.     HUD.mode = MBProgressHUDModeText; 
  82.      
  83.    //指定距离中心点的X轴和Y轴的偏移量,如果不指定则在屏幕中间显示 
  84. //    HUD.yOffset = 150.0f; 
  85. //    HUD.xOffset = 100.0f; 
  86.      
  87.     [HUD showAnimated:YES whileExecutingBlock:^{ 
  88.         sleep(2); 
  89.     } completionBlock:^{ 
  90.         [HUD removeFromSuperview]; 
  91.         [HUD release]; 
  92.         HUD = nil; 
  93.     }]; 

依次实现的效果如下:

                         


                         

下面这个效果就类似Android中的Toast:

                                                    

以上就简单介绍了MBProgressHUD的使用,这里都是采用block的形式来操作的,这样写起代码来更直观也更高效。

对Android&IOS感兴趣的朋友可以加入我们的讨论QQ群,在这里,我们只讨论干货:

iOS群:220223507

Android群:282552849


欢迎关注我的新浪微博和我交流:@唐韧_Ryan

0 0
原创粉丝点击