第三方框架MBProgressHUD-----实现各种提示框
程序运行显示如下 :
点击按钮实现对应的提示框:
这里只截取了其中一张图,有兴趣的可以自己运行程序,查看其他的几种提示框哟!!!
第三方框架MBProgressHUD的下载地址:https://github.com/jdg/MBProgressHUD
程序代码如下 :
//
// ViewController.m
// 第三方框架--提示框
//
// Created by mac1 on 15/10/5.
// Copyright (c) 2015年 www.iphonetrain.com. All rights reserved.
//
#import "ViewController.h"
#import "MBProgressHUD.h"
@interface ViewController ()
- (IBAction)textDialog:(id)sender;
- (IBAction)progressDialog1:(id)sender;
- (IBAction)progressDialog2:(id)sender;
- (IBAction)customDialog:(id)sender;
- (IBAction)allTextDialog:(id)sender;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
//文本提示框
- (IBAction)textDialog:(id)sender {
//创建进度框
__block MBProgressHUD *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 = nil; //block中改变对象值,__block
}];
}
//进度提示框一
- (IBAction)progressDialog1:(id)sender {
//创建进度框
__block MBProgressHUD *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.01;
hud.progress = progress;
usleep(50000);
}
} completionBlock:^{
//完成后执行的操作
[hud removeFromSuperview];
hud = nil;
}];
}
//进度提示框二
- (IBAction)progressDialog2:(id)sender {
//创建进度框
__block MBProgressHUD *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.01;
hud.progress = progress;
usleep(50000);
}
} completionBlock:^{
//完成后执行的操作
[hud removeFromSuperview];
hud = nil;
}];
}
//自定义进度框
- (IBAction)customDialog:(id)sender {
//创建进度框
__block MBProgressHUD *hud = [[MBProgressHUD alloc]initWithView:self.view];
[self.view addSubview:hud];
hud.labelText = @"操作成功";
hud.customView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"checkmark.png"]];
[hud showAnimated:YES whileExecutingBlock:^{
//休眠2秒
sleep(2);
} completionBlock:^{
[hud removeFromSuperview];
hud = nil;
}];
}
//纯文本提示框
- (IBAction)allTextDialog:(id)sender {
//创建进度框
__block MBProgressHUD *hud = [[MBProgressHUD alloc]initWithView:self.view];
[self.view addSubview:hud];
hud.labelText = @"你操作对了哟!!!";
hud.mode = MBProgressHUDModeText;
//指定显示文本的偏移量,不指定默认显示在屏幕中间
/*
hud.xOffset = 150;
hud.yOffset = 200;
*/
[hud showAnimated:YES whileExecutingBlock:^{
sleep(3);
} completionBlock:^{
[hud removeFromSuperview];
hud = nil;
}];
}
@end