本文转载至 http://blog.csdn.net/xunyn/article/details/8064984
 

原文地址http://www.189works.com/article-89289-1.html

MBProgressHUD 下载地址是: http://github.com/matej/MBProgressHUD

这里介绍一下网友开源的MBProgressHUD类,实现等待框,

一、网上下载  MBProgessHUD 类文件,直接导入到工程即可

二、示例分析

在我的工程中示例如下:

1)在ShowImageViewController.h头文件代码如下:

#import <UIKit/UIKit.h>

#import "MBProgressHUD.h"

@interface ShowImageViewController : UIViewController <MBProgressHUDDelegate>{

NSString         *_picUrlString;

UIImageView      *_imageView;

MBProgressHUD    *_progressHUD;

}

@property (nonatomic, copy) NSString           *picUrlString;

@property (nonatomic, retain) IBOutlet         UIImageView *imageView;

@property (nonatomic, retain) MBProgressHUD    *progressHUD;

//请求图片资源

-(void)imageResourceRequest;

//显示图片信息

-(void)displayImage:(UIImage *)image;

- (IBAction)dismissModealView:(id)sender;

-(void)removeModalView;

@end

2)在ShowImageViewController.m实现文件代码如下:

#import "ShowImageViewController.h"

#import <QuartzCore/QuartzCore.h>

@implementation ShowImageViewController

@synthesize picUrlString = _picUrlString;

@synthesize imageView = _imageView;

@synthesize progressHUD = _progressHUD;

- (void)viewDidLoad

{

[super viewDidLoad];

// Do any additional setup after loading the view from its nib.

self.view.backgroundColor = [UIColor grayColor];

self.view.alpha = 0.8;

//设置图片为圆角

self.imageView.backgroundColor = [UIColor clearColor];

self.imageView.layer.borderColor = [UIColor lightGrayColor].CGColor;

self.imageView.layer.borderWidth = 5.0;

self.imageView.layer.masksToBounds = YES;

self.imageView.layer.cornerRadius = 10.0;

}

-(void)viewWillAppear:(BOOL)animated

{

[super viewWillAppear:animated];

//当进入视图时,重新设置imageView

[self.imageView setImage:nil];

[self.imageView setFrame:CGRectMake(160, 200, 0, 0)];

//显示加载等待框

self.progressHUD = [[MBProgressHUD alloc] initWithView:self.view];

[self.view addSubview:self.progressHUD];

[self.view bringSubviewToFront:self.progressHUD];

self.progressHUD.delegate = self;

self.progressHUD.labelText = @"加载中...";

[self.progressHUD show:YES];

//开启线程,请求图片资源

[NSThread detachNewThreadSelector:@selector(imageResourceRequest) toTarget:selfwithObject:nil];

}

//请求图片资源

-(void)imageResourceRequest

{

NSAutoreleasePool   *pool = [[NSAutoreleasePool alloc] init];

//根据网络数据,获得到image资源

NSData  *data = http://www.cnblogs.com/snake-hand/archive/2012/08/13/[[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:self.picUrlString]];

UIImage *image = [[UIImage alloc] initWithData:data];

[data release];

//回到主线程,显示图片信息

[self performSelectorOnMainThread:@selector(displayImage:) withObject:imagewaitUntilDone:NO];

[image release];

[pool release];

}

//显示图片信息

-(void)displayImage:(UIImage *)image

{

//若self.progressHUD为真,则将self.progressHUD移除,设为nil

if (self.progressHUD){

[self.progressHUD removeFromSuperview];

[self.progressHUD release];

self.progressHUD = nil;

}

//图片慢慢放大动画效果

[self.imageView setImage:image];

[UIView beginAnimations:nil context:nil];

[UIView setAnimationDuration:0.5];

[self.imageView setFrame:CGRectMake(40, 100, 240, 160)];

[UIView commitAnimations];

}

- (void)viewDidUnload

{

[self setImageView:nil];

[super viewDidUnload];

// Release any retained subviews of the main view.

// e.g. self.myOutlet = nil;

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

// Return YES for supported orientations

return (interfaceOrientation == UIInterfaceOrientationPortrait);

}

- (IBAction)dismissModealView:(id)sender {

//设置定时器,当动画结束时,子视图从父视图中移除

[NSTimer scheduledTimerWithTimeInterval:0.5 target:selfselector:@selector(removeModalView) userInfo:nil repeats:NO];

[UIView beginAnimations:nil context:nil];

[UIView setAnimationDuration:0.5];

[self.imageView setFrame:CGRectMake(160, 200, 0, 0)];

[UIView commitAnimations];

}

-(void)removeModalView

{

[self.view removeFromSuperview];

}

#pragma mark -

#pragma mark MBProgressHUDDelegate methods

- (void)hudWasHidden:(MBProgressHUD *)hud {

NSLog(@"Hud: %@", hud);

// Remove HUD from screen when the HUD was hidded

[self.progressHUD removeFromSuperview];

[self.progressHUD release];

self.progressHUD = nil;

}

- (void)dealloc

{

[_picUrlString release];

[_imageView release];

[super dealloc];

}

@end

三、效果展示

四、总结

利用MBProgressHUD实现加载等待框,视觉效果大大提高

IOS开发UI篇之──自定义加载等待框(MBProgressHUD)的更多相关文章

  1. iOS开发UI篇-懒加载、重写setter方法赋值

    一.懒加载 1.懒加载定义 懒加载——也称为延迟加载,即在需要的时候才加载(效率低,占用内存小).所谓懒加载,写的是其get方法. 注意:如果是懒加载的话则一定要注意先判断是否已经有了,如果没有那么再 ...

  2. iOS开发UI篇—Quartz2D(自定义UIImageView控件)

    iOS开发UI篇—Quartz2D(自定义UIImageView控件) 一.实现思路 Quartz2D最大的用途在于自定义View(自定义UI控件),当系统的View不能满足我们使用需求的时候,自定义 ...

  3. IOS开发UI篇之──自定义UIActionSheet

    转载自:http://www.cnblogs.com/pengyingh/articles/2343200.html UIActionSheet类系IOS开发中实现警告框的重要的类,而在好多应用中,都 ...

  4. iOS开发UI篇—懒加载

    iOS开发UI篇—懒加载 1.懒加载基本 懒加载——也称为延迟加载,即在需要的时候才加载(效率低,占用内存小).所谓懒加载,写的是其get方法. 注意:如果是懒加载的话则一定要注意先判断是否已经有了, ...

  5. ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局

    本文转自 :http://www.cnblogs.com/wendingding/p/3761730.html ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布 ...

  6. iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局

    iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局 一.项目文件结构和plist文件 二.实现效果 三.代码示例 1.没有使用配套的类,而是直接使用xib文 ...

  7. iOS开发UI篇—CAlayer(自定义layer)

    iOS开发UI篇—CAlayer(自定义layer) 一.第一种方式 1.简单说明 以前想要在view中画东西,需要自定义view,创建一个类与之关联,让这个类继承自UIView,然后重写它的Draw ...

  8. iOS开发UI篇—transframe属性(形变)

    iOS开发UI篇—transframe属性(形变) 1. transform属性 在OC中,通过transform属性可以修改对象的平移.缩放比例和旋转角度 常用的创建transform结构体方法分两 ...

  9. iOS开发UI篇—简单的浏览器查看程序

    iOS开发UI篇—简单的浏览器查看程序 一.程序实现要求 1.要求 2. 界面分析 (1) 需要读取或修改属性的控件需要设置属性 序号标签 图片 图片描述 左边按钮 右边按钮 (2) 需要监听响应事件 ...

随机推荐

  1. 转: 私人珍藏的Chrome插件,吐血推荐

    转:来自 http://stormzhang.com/devtools/2016/01/15/google-chrome-extension/

  2. Rails生成随机字符串及加解密

    1.生成安全的base64字符串 key = SecureRandom.urlsafe_base64

  3. 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-人机界面如何自动运行,不让用户干涉,设置起始界面

    右击视图管理器,添加一个TargetVisualization   在起始视图中点击右边的按钮,然后选择一个HMI作为起始HMI     更多教学视频和资料下载,欢迎关注以下信息: 我的优酷空间: h ...

  4. 播放器设置 Player Settings

    原地址:http://game.ceeger.com/Manual/class-PlayerSettings.html#Android Player Settings is where you def ...

  5. lodash round

    _.round(number, [precision=0]) 根据 precision 四舍五入 number. _.round(4.006); // => 4 _.round(4.006, 2 ...

  6. (二)Linux——Linux常用指令

    1. 文件目录操作命令 ls 显示文件和目录列表     -l 列出文件的详细信息 -a 列出当前目录所有文件,包含隐藏文件 mkdir 创建目录 -p 父目录不存在情况下先生成父目录 cd 切换目录 ...

  7. (一)Linux——Linux基本概念

    Linux是一种自由和开放源码的类UNIX操作系统,使用Linux内核.目前存在着许多不同的Linux发行版,可安装在各种各样的电脑硬件设备,从手机.平板电脑.路由器和影音游戏控制台,到桌上型电脑,大 ...

  8. 【MyBatis学习01】宏观上把握MyBatis框架

    今天开始学习mybatis框架,博客主要记录学习过程中的一些总结,如有错误之处,欢迎留言指正~先用mybatis的鸟鸟来镇个楼,咳咳~~ mybatis框架是一个持久层框架,是Apache下的顶级项目 ...

  9. c#上一周下一周代码

    public partial class Form1 : Form { DateTime dtNow; public Form1() { InitializeComponent(); } privat ...

  10. netstat命令初探

    Proto :网络传输协议,主要为tcp和udp Local Address :本地的ip:port Foreign Address:远程主机的ip:port State :连线状态,主要有监听( L ...