IOS开发UI篇之──自定义加载等待框(MBProgressHUD)
来源:互联网 发布:民生银行软件下载 编辑:程序博客网 时间:2024/06/10 08:08
转自:http://www.189works.com/article-89289-1.html
一、网上下载 MBProgessHUD 类文件,直接导入到工程即可
下载地址是: http://github.com/matej/MBProgressHUD
二、示例分析
在我的工程中示例如下:
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
{
[superviewDidLoad];
// Do any additional setup after loading the view from its nib.
self.view.backgroundColor = [UIColorgrayColor];
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
{
[superviewWillAppear: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];
//开启线程,请求图片资源
[NSThreaddetachNewThreadSelector:@selector(imageResourceRequest)toTarget:selfwithObject:nil];
}
//请求图片资源
-(void)imageResourceRequest
{
NSAutoreleasePool *pool = [[NSAutoreleasePoolalloc] init];
//根据网络数据,获得到image资源
NSData *data = http://www.cnblogs.com/snake-hand/archive/2012/08/13/[[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:self.picUrlString]];
UIImage *image = [[UIImagealloc] initWithData:data];
[data release];
//回到主线程,显示图片信息
[selfperformSelectorOnMainThread:@selector(displayImage:)withObject:image waitUntilDone: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];
[UIViewbeginAnimations:nilcontext:nil];
[UIViewsetAnimationDuration:0.5];
[self.imageView setFrame:CGRectMake(40,100, 240, 160)];
[UIViewcommitAnimations];
}
- (void)viewDidUnload
{
[selfsetImageView:nil];
[superviewDidUnload];
// 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 {
//设置定时器,当动画结束时,子视图从父视图中移除
[NSTimerscheduledTimerWithTimeInterval:0.5target:selfselector:@selector(removeModalView)userInfo:nilrepeats:NO];
[UIViewbeginAnimations:nilcontext:nil];
[UIViewsetAnimationDuration:0.5];
[self.imageView setFrame:CGRectMake(160,200, 0, 0)];
[UIViewcommitAnimations];
}
-(void)removeModalView
{
[self.viewremoveFromSuperview];
}
#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];
[superdealloc];
}
@end
三、效果展示
四、总结
利用MBProgressHUD实现加载等待框,视觉效果大大提高
- IOS开发UI篇之──自定义加载等待框(MBProgressHUD)
- IOS开发UI篇之──自定义加载等待框(MBProgressHUD)
- IOS开发UI篇之──自定义加载等待框(MBProgressHUD)
- IOS开发UI篇之──自定义加载等待框(MBProgressHUD)
- IOS开发UI篇之──自定义加载等待框(MBProgressHUD)
- IOS开发UI篇之──自定义加载等待框(MBProgressHUD)
- IOS开发UI篇之──自定义加载等待框(MBProgressHUD)
- IOS开发UI篇之──自定义加载等待框(MBProgressHUD)
- iOS开发之自定义加载等待框(MBProgressHUD)
- IOS--─自定义加载等待框(MBProgressHUD)
- 自定义加载等待框(MBProgressHUD)
- 自定义加载等待框(MBProgressHUD)
- iOS自定义加载等待视图-MBProgressHUD
- MBProgressHUD自定义等待框
- IOS开发UI篇之──自定义UIActionSheet
- IOS开发UI篇之──自定义UIActionSheet
- IOS开发UI篇之──自定义UIActionSheet
- iOS开发之MBProgressHUD
- Debug与Release
- Android 学习笔记(十三):Activity-GridView
- Android 学习笔记(十四):Activity-AutoCompleteTextView
- 读取DMI信息
- 2011-06-19 13:55 VC++6.0 SP6 补丁(解决Link时假死现象)
- IOS开发UI篇之──自定义加载等待框(MBProgressHUD)
- android学习之R文件消失
- [转]Stage3D 翻译系列之四:了解顶点着色器和片段(像素)着色器
- eval 解析json报错 提示缺少“;”
- Android 学习笔记(十五):Activity-GalleryView
- error C2440 “static_cast” 无法从“void (__thiscall CPppView )(void)”转换为“LRESULT (__thiscall
- 反客为主
- 黑马程序员_java基础(一):Java简介和入门注意事项
- PHP 安装 MessagePack