iOS MBProgressHUD 之带底板的加载提示

文章来自:http://blog.csdn.net/ryantang03/article/details/7877120

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. @interface ViewController : UIViewController
  4. {
  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. @end

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

[cpp] view plaincopy

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

依次实现的效果如下:

               

               

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

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

时间: 2024-08-25 15:23:57

iOS MBProgressHUD 之带底板的加载提示的相关文章

iOS和tvOS游戏按需加载资源简介

摘要 与iOS 9和watchOS 2一起,苹果引入了一套新的内容分发API,以便节约设备空间,这就是按需加载资源.通过使用按需加载资源,我们可以将特定的应用程序资源托管在苹果的服务器上,然后在需要的时候进行加载.在这个教程中,我将通过开发一个图片查看应用介绍一下按需加载资源的基本用法. tvOS On Demand Reourse 按需加载 iOS开发 目录[-] 介绍 准备工作 1. 按需加载资源 益处 类别 限制 应用分片 删除按需加载资源 2. 分配和指定Tag 3. 访问按需请求资源

ios网络学习------4 UIWebView的加载本地数据的三种方式

UIWebView是IOS内置的浏览器,可以浏览网页,打开文档  html/htm  pdf   docx  txt等格式的文件.  safari浏览器就是通过UIWebView做的. 服务器将MIME的标识符等放入传送的数据中告诉浏览器使用那种插件读取相关文件. uiwebview加载各种本地文件(通过loadData方法): - (void)viewDidLoad { [super viewDidLoad]; [self setupUI]; NSString *path = [[NSBund

jQuery Ajax封装(附带加载提示和请求结果提示模版)

1.创建HTML文件(demo) <!doctype html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>jQuery Ajax</title> <script type="text/javascript" src="jquery-3.2.0.min.js"></sc

IOS开发UI篇之──自定义加载等待框(MBProgressHUD)

本文转载至 http://blog.csdn.net/xunyn/article/details/8064984 原文地址http://www.189works.com/article-89289-1.html MBProgressHUD 下载地址是: http://github.com/matej/MBProgressHUD 这里介绍一下网友开源的MBProgressHUD类,实现等待框, 一.网上下载  MBProgessHUD 类文件,直接导入到工程即可 二.示例分析 在我的工程中示例如下

ios navigationcontroller 滑动返回与webview加载html图片自适应屏幕宽度

1 .ios navigationcontroller 滑动返回 滑动返回是navigationcontroller默认返回按钮自带的功能,如果返回按钮自定义该功能失效, 解决的办法有两个: ① self.navigationItem.backBarButtonItem =   [[UIBarButtonItem alloc]initWithCustomView:button];//这个方法用不了 只能用 self.navigationItem.backBarButtonItem = [ [UI

iOS开发&gt;学无止境 - Cell异步图片加载优化,缓存机制详解

作者:勤奋的笨老头 网址:http://www.jianshu.com/p/02ab2b74c451 最近研究了一下UITbleView中异步加载网络图片的问题,iOS应用经常会看到这种界面.一个tableView上显示一些标题.详情等内容,在加上一张图片.这里说一下这种思路. 为了防止图片多次下载,我们需要对图片做缓存,缓存分为内存缓存于沙盒缓存,我们当然两种都要实现. 由于tableViewCell是有重用机制的,也就是说,内存中只有当前可见的cell数目的实例,滑动的时候,新显示cell会

iOS开发之控制器创建与加载(生命周期)

1.如何创建一个控制器 控制器常见的创建方式有以下几种: (1)通过storyboard创建 (2)直接创建 MJViewController *mj = [[MJViewController alloc] init]; (3)指定xib文件来创建 MJViewController *mj = [[MJViewController alloc] initWithNibName: @"MJViewController" bundle:nil]; 注意,创建xid后还要设置xid中哪个vi

[YII2] 去除自带js,加载自己的JS,然后ajax(json)传值接值!

本想用YII2自带的JS,可是用着效果不好,想从新加载,找了好多终于实现啦!还有ajax(json)传值接值! 首先直接了当的就把YII2自带的js去掉! 把下面代码加入到/config/main.php文件'components'=>[]里面,可以禁掉CSS和JS 1 'components' => [ 2 ............. 3 //不加载YII2自带JS以及CSS 4 'assetManager'=>[ 5 'bundles'=>[ 6 'yii\bootstrap\

iOS开发之0行代码加载NSBundle中的@2x与@3x图片

本文只针对通过NSBundle对象的方法 pathForResource 获取本地图片资源遇到的图片名无法自动识别@2x与@3x名称的问题进行测试.总结与分享. 加载本地图片资源的方式一般通过以下两种方法: 第1种: UIImage *img = [UIImage imageNamed:@"imageName"]; 第2种: UIImage *img = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForRes