这里介绍一下网友开源的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:self withObject:nil];

}

//请求图片资源

-(void)imageResourceRequest

{

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

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

NSData  *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:self.picUrlString]];

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

[data release];

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

[self performSelectorOnMainThread:@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];

[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:self selector:@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实现加载等待框,视觉效果大大提高

转载于:https://www.cnblogs.com/snake-hand/archive/2012/08/13/2636219.html

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

  1. 自定义加载等待框(MBProgressHUD)

    一.网上下载  MBProgessHUD 类文件,直接导入到工程即可 二.示例分析 在我的工程中示例如下: 1)在ShowImageViewController.h头文件代码如下: #import & ...

  2. iOS开发UI篇-在UItableview中实现加载更多功能

    iOS开发UI篇-在UItableview中实现加载更多功能 一.实现效果 点击加载更多按钮,出现一个加载图示,三秒钟后添加两条新的数据. 二.实现代码和说明 当在页面(视图部分)点击加载更多按钮的时 ...

  3. iOS开发UI篇—懒加载

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

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

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

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

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

  6. iOS开发UI篇—实现UITableview控件数据刷新

    iOS开发UI篇-实现UITableview控件数据刷新 一.项目文件结构和plist文件 二.实现效果 1.说明:这是一个英雄展示界面,点击选中行,可以修改改行英雄的名称(完成数据刷新的操作). 运 ...

  7. iOS开发UI篇—UITableviewcell的性能优化和缓存机制

    iOS开发UI篇-UITableviewcell的性能问题 一.UITableviewcell的一些介绍 UITableView的每一行都是一个UITableViewCell,通过dataSource ...

  8. iOS开发UI篇—UITableview控件基本使用

    iOS开发UI篇-UITableview控件基本使用 一.一个简单的英雄展示程序 NJHero.h文件代码(字典转模型) 1 #import <Foundation/Foundation.h&g ...

  9. iOS开发UI篇—UITableview控件使用小结

    iOS开发UI篇-UITableview控件使用小结 一.UITableview的使用步骤 UITableview的使用就只有简单的三个步骤: 1.告诉一共有多少组数据 方法:- (NSInteger ...

最新文章

  1. 张立贤:积跬步至千里,我与地学大数据的探索之旅 | 提升之路系列(五)
  2. Redis数据类型(String)
  3. 使用SC命令使用(转)
  4. LeetCode 5832. 构造元素不等于两相邻元素平均值的数组
  5. python中int input_python中的input是什么
  6. 面向对象程序设计要考虑的7个原则
  7. PCL中使用FLANN库(2)
  8. J2SE7规范_2013.2_类型_命名
  9. mixpanel实验教程(2)
  10. Rancher某环境所有主机网络瘫痪问题
  11. SEO人员,为什么要做流量过滤,如何操作?
  12. 图标、协同办公等新素材上线,100%实用!
  13. JS的DOM操作3--删除事件,注册事件与冒泡⭐⭐⭐(附带动图案例)
  14. 德国交通标志检测识别数据集
  15. 4k纸是几厘米乘几厘米_4k纸多大,纸张的大小规格
  16. centos8 免密登录
  17. 摄像头poe供电原理_如何区分摄像头是否支持PoE供电摄像头?
  18. 【蓝桥杯单片机备赛】3.【SMG】共阳共阴数码管模板整理及真题实战心得
  19. 1970 matla 时间_在MATLAB中绘制时间序列数据
  20. 2021中国最好学科排名:北大、清华、人大中国顶尖学科居前三

热门文章

  1. java实现表达式求值_如何编写一个高效的Java表达式求值程序
  2. ppt修复无法读取_移动硬盘故障分析以及建议修复方法
  3. C语言代码规范(十)花里胡哨代码鉴赏
  4. C语言代码规范(六)浮点型变量逻辑比较
  5. 图像分割-基本边缘检测roberts,prewitt,sobel,canny,laplace
  6. PyCharm安装及配置
  7. 计算机二级C语言易混淆的区别
  8. ++i与i++的根本性区别(两个代码对比搞定)
  9. 方法之输出星型及其调用
  10. oracle查看数据库是否恢复成功_记一次解决docker下oracle数据库故障事例