zoukankan      html  css  js  c++  java
  • 【转】IOS学习笔记29—提示框第三方库之MBProgressHUD

    原文网址:http://blog.csdn.net/ryantang03/article/details/7877120

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


    依次实现的效果如下:

                              

     

                              

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

                                                         

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

     

    加入我们的QQ群或微信公众账号请查看:Ryan's zone公众账号及QQ群

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

  • 相关阅读:
    PHP面向对象——类
    PHP强大的数组函数
    php学习资源
    版本管理(二)之Git和GitHub的连接和使用
    版本管理(一)之Git和GitHub的区别(优点和缺点)
    (win10)Wamp环境下php升级至PHP7.2
    wamp3.1.0 X64下载链接
    4.总结近5周以来的github上的工作情况,以图表方式分析你小组的工作情况、存在的问题及解决的方案。(尤心心)
    四则运算需求分析和功能实现--杨宇杰
    1.对四则运算软件需求的获取方式进行实践,例如使用调查问卷访问相关关系人等。
  • 原文地址:https://www.cnblogs.com/wi100sh/p/6179586.html
Copyright © 2011-2022 走看看