摘要:本文将带你了解IOS开发之MBProgressHUD是什么,希望本文对大家学IOS有所帮助。
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的形式来操作的,这样写起代码来更直观也更高效。
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之IOS频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号