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) 需要监听响应事件 ...
随机推荐
- C#文本框中默认是不允许使用全选的
C#文本框中默认是不允许使用全选的.可以通过以下事件完成: private void textBox1_KeyDown(object sender, KeyEventArgs e) { i ...
- proto3 中的 map 类型
.proto syntax = "proto3"; option optimize_for = SPEED; message TestStruct { map<int32,s ...
- 说说C#的数学类,Math,浮点数(上)
说说C#的数学类,Math,浮点数 C#语言支持下图所看到的的数值类型,各自是整数,浮点数和小数 可能不是非常清楚,可是细致看看还是能看清楚的. 在一个C#程序中,整数(没有小数点的数)被觉得是一个i ...
- 04-1下载Win系统(装机助理)
下载Win系统(装机助理): http://www.zhuangjizhuli.com/upan.html http://www.krlxx.com/64win7.html 选择你需要安装的系统: 以 ...
- Unlink of file 'xx' failed. Should I try again? (y/n) 解决办法
Unlink of file 'xx' failed. Should I try again? (y/n) 原因:一般遇到这个错输入y/n都不能解决问题,出现这个问题的原因可能是其他程序正在操作git ...
- 硬件(MAC)地址的概念及作用
概念:MAC地址就是在媒体接入层上使用的地址,也叫物理地址.硬件地址或链路地址,其被固化在适配器的ROM中. 可见MAC地址实际上就是适配器地址或适配器标识符.当某台计算机使用某块适配器后,适配器上的 ...
- TIME_WAIT状态及存在原因
1. 客户端与服务器端建立TCP/IP连接后关闭SOCKET后,服务器端连接的端口状态为TIME_WAIT: 2. 主动关闭的Socket端会进入TIME_WAIT状态,并且持续2MSL时间长度,MS ...
- LVM详解笔记pv-vg-lv创建和扩展
LVM Logical Volume Manager(逻辑卷管理) 是Linux环境下对底层磁盘的一种管理机制(方式),处在物理磁盘和文件系统之间. 名词: PV (Physical Volume)物 ...
- NIO之阻塞IO与非阻塞IO(包含Selector使用)
阻塞IO 传统的 IO 流都是阻塞式的. 也就是说,当一个线程调用 read() 或 write()时,该线程被阻塞,直到有一些数据被读取或写入,该线程在此期间不能执行其他任务. 因此,在完成网络通信 ...
- C# 运行时中的泛型
将泛型类型或方法编译为 Microsoft 中间语言 (MSIL) 时,它包含将其标识为具有类型参数的元数据. 泛型类型的 MSIL 的使用因所提供的类型参数是值类型还是引用类型而不同. 第一次用值类 ...