IOS开发UI篇之──自定义加载等待框(MBProgressHUD)
原文地址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)的更多相关文章
- iOS开发UI篇-懒加载、重写setter方法赋值
一.懒加载 1.懒加载定义 懒加载——也称为延迟加载,即在需要的时候才加载(效率低,占用内存小).所谓懒加载,写的是其get方法. 注意:如果是懒加载的话则一定要注意先判断是否已经有了,如果没有那么再 ...
- iOS开发UI篇—Quartz2D(自定义UIImageView控件)
iOS开发UI篇—Quartz2D(自定义UIImageView控件) 一.实现思路 Quartz2D最大的用途在于自定义View(自定义UI控件),当系统的View不能满足我们使用需求的时候,自定义 ...
- IOS开发UI篇之──自定义UIActionSheet
转载自:http://www.cnblogs.com/pengyingh/articles/2343200.html UIActionSheet类系IOS开发中实现警告框的重要的类,而在好多应用中,都 ...
- iOS开发UI篇—懒加载
iOS开发UI篇—懒加载 1.懒加载基本 懒加载——也称为延迟加载,即在需要的时候才加载(效率低,占用内存小).所谓懒加载,写的是其get方法. 注意:如果是懒加载的话则一定要注意先判断是否已经有了, ...
- ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局
本文转自 :http://www.cnblogs.com/wendingding/p/3761730.html ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布 ...
- iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局
iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局 一.项目文件结构和plist文件 二.实现效果 三.代码示例 1.没有使用配套的类,而是直接使用xib文 ...
- iOS开发UI篇—CAlayer(自定义layer)
iOS开发UI篇—CAlayer(自定义layer) 一.第一种方式 1.简单说明 以前想要在view中画东西,需要自定义view,创建一个类与之关联,让这个类继承自UIView,然后重写它的Draw ...
- iOS开发UI篇—transframe属性(形变)
iOS开发UI篇—transframe属性(形变) 1. transform属性 在OC中,通过transform属性可以修改对象的平移.缩放比例和旋转角度 常用的创建transform结构体方法分两 ...
- iOS开发UI篇—简单的浏览器查看程序
iOS开发UI篇—简单的浏览器查看程序 一.程序实现要求 1.要求 2. 界面分析 (1) 需要读取或修改属性的控件需要设置属性 序号标签 图片 图片描述 左边按钮 右边按钮 (2) 需要监听响应事件 ...
随机推荐
- Spark(十一) -- Mllib API编程 线性回归、KMeans、协同过滤演示
本文测试的Spark版本是1.3.1 在使用Spark的机器学习算法库之前,需要先了解Mllib中几个基础的概念和专门用于机器学习的数据类型 特征向量Vector: Vector的概念是和数学中的向量 ...
- 解决防火墙限制远程连接MySQL(导致错误10060可能之一)
打开windows防火墙,打开高级设置 1. 入站规则设置 ① 选择入站规则,然后新建规则,选择端口,然后下一步 ② 选择TCP,选择特定端口,然后输入端口,如有多个端口需要用逗号隔开了 例如: 33 ...
- Java Volatile keyword
Volatile修饰的成员变量在每次被线程訪问时,都强迫从主内存中重读该成员变量的值.并且,当成员变量发生变化时,强迫线程将变化值回写到主内存.这样在不论什么时刻,两个不同的线程总是看到某个成员变量的 ...
- Linux——Ubuntu下Sublime Text 2的安装
Sublime Text 2是一款共享软件,收费但可以永久免费试用的编辑器,价格是59个美刀,相信开发者一定不了解中国人,也不面对中国市场,.言归正传,ST2编辑功能强大,好评如潮,在Windows/ ...
- T420s成功加装固态硬盘(SSD)
目的 为了提升系统和经常使用工具的启动速度,ThinkPad T420s光驱位加一块固态硬盘. 操作步骤 购买:没做太多对照了解,初步计划是安装在光驱位,直接JD上买了SanDisk的128G和推荐的 ...
- (三)Maven基本概念——常用插件的配置
看注释———— pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http ...
- Spring boot centos部署启动停止脚本
原文地址:http://www.cnblogs.com/skyblog/p/7243979.html 使用脚本启动和关闭服务,centos下的脚本启动和关闭可以如下: start(){ now=`da ...
- 虚拟机实现https网络设置
现在很多网站使用的都是https协议,想在自己的电脑上实现下, 由于自己的电脑是win10,我总是觉得在windows上布置环境不如在linux上稳定,所以在电脑上安装了虚拟机,cento系统 . 上 ...
- html标签说明
dictype 不区分大小写 HTML 4.01 与 HTML5 之间的差异 在 HTML 4.01 中有三种 <!DOCTYPE> 声明.在 HTML5 中只有一种: <!DOCT ...
- Sublime text 3 搭建Python3 IDE
起因:为了提高编码工作中的体验,Sublime Text:不仅具有华丽的界面,还支持插件扩展机制,用她来写代码,绝对是一种享受. Vim难于上手,Eclipse,VS 体积庞大,即便体积轻巧迅速启动的 ...