MBProgressHUD 详解

         今天开发遇到的问题,用C#语言开发iOS 应用程序,本来都不怎熟悉,今天又遇到了这个MBProgressHUD 自定义坐标的问题,在网上查看之后,有了解决办法,纪录下来,工作中的小问题。
MBProgressHUD是一个开源项目,实现了很多种样式的提示框,使用上简单、方便,并且可以对显示的内容进行自定义,功能很强大,很多项目中都有使用到。到GitHub上可以下载到项目源码 https://github.com/jdg/MBProgressHUD ,下载下来后直接把MBProgressHUD.h和MBProgressHUD.m拖入工程中就行,别忘了选择拷贝到工程。完了在需要使用的地方导入头文件就可以开始使用了。首先看下工程截图:

                                                                

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

                                                                         

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

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

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

[cpp]  view plain copy
  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.   
  24. - (IBAction)showProgressDialog:(id)sender {  
  25.     HUD = [[MBProgressHUD alloc] initWithView:self.view];  
  26.     [self.view addSubview:HUD];  
  27.     HUD.labelText = @"正在加载";  
  28.       
  29.     //设置模式为进度框形的  
  30.     HUD.mode = MBProgressHUDModeDeterminate;  
  31.     [HUD showAnimated:YES whileExecutingBlock:^{  
  32.         float progress = 0.0f;  
  33.         while (progress < 1.0f) {  
  34.             progress += 0.01f;  
  35.             HUD.progress = progress;  
  36.             usleep(50000);  
  37.         }  
  38.     } completionBlock:^{  
  39.         [HUD removeFromSuperview];  
  40.         [HUD release];  
  41.         HUD = nil;  
  42.     }];  
  43. }  
  44.   
  45. - (IBAction)showProgressDialog2:(id)sender {  
  46.     HUD = [[MBProgressHUD alloc] initWithView:self.view];  
  47.     [self.view addSubview:HUD];  
  48.     HUD.labelText = @"正在加载";  
  49.     HUD.mode = MBProgressHUDModeAnnularDeterminate;  
  50.       
  51.     [HUD showAnimated:YES whileExecutingBlock:^{  
  52.         float progress = 0.0f;  
  53.         while (progress < 1.0f) {  
  54.             progress += 0.01f;  
  55.             HUD.progress = progress;  
  56.             usleep(50000);  
  57.         }  
  58.     } completionBlock:^{  
  59.         [HUD removeFromSuperview];  
  60.         [HUD release];  
  61.         HUD = nil;  
  62.     }];  
  63. }  
  64.   
  65. - (IBAction)showCustomDialog:(id)sender {  
  66.     HUD = [[MBProgressHUD alloc] initWithView:self.view];  
  67.     [self.view addSubview:HUD];  
  68.     HUD.labelText = @"操作成功";  
  69.     HUD.mode = MBProgressHUDModeCustomView;  
  70.     HUD.customView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Checkmark"]] autorelease];  
  71.     [HUD showAnimated:YES whileExecutingBlock:^{  
  72.         sleep(2);  
  73.     } completionBlock:^{  
  74.         [HUD removeFromSuperview];  
  75.         [HUD release];  
  76.         HUD = nil;  
  77.     }];  
  78.       
  79. }  

  80. - (IBAction)showAllTextDialog:(id)sender {  
  81.     HUD = [[MBProgressHUD alloc] initWithView:self.view];  
  82.     [self.view addSubview:HUD];  
  83.     HUD.labelText = @"操作成功";  
  84.     HUD.mode = MBProgressHUDModeText;  
  85.       
  86.     //指定距离中心点的X轴和Y轴的偏移量,如果不指定则在屏幕中间显示  
  87. //    HUD.yOffset = 150.0f;  
  88. //    HUD.xOffset = 100.0f;  
  89.       
  90.     [HUD showAnimated:YES whileExecutingBlock:^{  
  91.         sleep(2);  
  92.     } completionBlock:^{  
  93.         [HUD removeFromSuperview];  
  94.         [HUD release];  
  95.         HUD = nil;  
  96.     }];  
  97. }  
  98. // 自定义提示框

                _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


 */

当你在开发中遇到问题时,最好的解决办法就是冷静思考,一层一层摸索问题的根源,希望自己能在这里继续坚持下去!加油!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值