首先MBProgressHUD是一个第三方框架,主要作用为应用显示一个过渡的作用,常用于打开一个联网页面加载过程,防止出现假死现象,如果网速慢则告诉用户已经在很努力很努力的加载中。GitHub上面的下载地址:https://github.com/jdg/MBProgressHUD
源码中也自带了一个Demo,显示13中动画效果,可以根据需要选取其中特效加以使用,使用方法基本一样;使用的时候只加把MBProgressHUD.h和MBProgressHUD.m拖入工程中,在使用的文件中加上#import"MBProgressHUD.h"
- #import <UIKit/UIKit.h>
- #import "MBProgressHUD.h"
- @interface ViewController : UIViewController
- {
- //HUD(Head-Up Display,意思是抬头显示的意思)
- MBProgressHUD *HUD;
- }
- - (IBAction)showTextDialog:(id)sender;
- - (IBAction)showProgressDialog:(id)sender;
- - (IBAction)showProgressDialog2:(id)sender;
- - (IBAction)showCustomDialog:(id)sender;
- - (IBAction)showAllTextDialog:(id)sender;
- @end
- - (IBAction)showTextDialog:(id)sender {
- //初始化进度框,置于当前的View当中
- HUD = [[MBProgressHUD alloc] initWithView:self.view];
- [self.view addSubview:HUD];
- //如果设置此属性则当前的view置于后台
- HUD.dimBackground = YES;
- //设置对话框文字
- HUD.labelText = @"请稍等";
- //显示对话框
- [HUD showAnimated:YES whileExecutingBlock:^{
- //对话框显示时需要执行的操作
- sleep(3);
- } completionBlock:^{
- //操作执行完后取消对话框
- [HUD removeFromSuperview];
- [HUD release];
- HUD = nil;
- }];
- }
- - (IBAction)showProgressDialog:(id)sender {
- HUD = [[MBProgressHUD alloc] initWithView:self.view];
- [self.view addSubview:HUD];
- HUD.labelText = @"正在加载";
- //设置模式为进度框形的
- HUD.mode = MBProgressHUDModeDeterminate;
- [HUD showAnimated:YES whileExecutingBlock:^{
- float progress = 0.0f;
- while (progress < 1.0f) {
- progress += 0.01f;
- HUD.progress = progress;
- usleep(50000);
- }
- } completionBlock:^{
- [HUD removeFromSuperview];
- [HUD release];
- HUD = nil;
- }];
- }
- - (IBAction)showProgressDialog2:(id)sender {
- HUD = [[MBProgressHUD alloc] initWithView:self.view];
- [self.view addSubview:HUD];
- HUD.labelText = @"正在加载";
- HUD.mode = MBProgressHUDModeAnnularDeterminate;
- [HUD showAnimated:YES whileExecutingBlock:^{
- float progress = 0.0f;
- while (progress < 1.0f) {
- progress += 0.01f;
- HUD.progress = progress;
- usleep(50000);
- }
- } completionBlock:^{
- [HUD removeFromSuperview];
- [HUD release];
- HUD = nil;
- }];
- }
- - (IBAction)showCustomDialog:(id)sender {
- HUD = [[MBProgressHUD alloc] initWithView:self.view];
- [self.view addSubview:HUD];
- HUD.labelText = @"操作成功";
- HUD.mode = MBProgressHUDModeCustomView;
- HUD.customView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Checkmark"]] autorelease];
- [HUD showAnimated:YES whileExecutingBlock:^{
- sleep(2);
- } completionBlock:^{
- [HUD removeFromSuperview];
- [HUD release];
- HUD = nil;
- }];
- }
- - (IBAction)showAllTextDialog:(id)sender {
- HUD = [[MBProgressHUD alloc] initWithView:self.view];
- [self.view addSubview:HUD];
- HUD.labelText = @"操作成功";
- HUD.mode = MBProgressHUDModeText;
- //指定距离中心点的X轴和Y轴的偏移量,如果不指定则在屏幕中间显示
- // HUD.yOffset = 150.0f;
- // HUD.xOffset = 100.0f;
- [HUD showAnimated:YES whileExecutingBlock:^{
- sleep(2);
- } completionBlock:^{
- [HUD removeFromSuperview];
- [HUD release];
- HUD = nil;
- }];
- }