提示框框架KVNProgress介绍
gitHub上面有很多显示加载进度的框架,这里我们介绍一下KVNProgress框架,KVNProgress是一个可以完全定制的HUD(指示器),你可以设置加载进度的画面是否全屏,可以自己修改进度显示的风格,同时这个框架也支持在ipad上面使用,效果如下:
一、导入KVNProgress框架
- 在github上面下载框架,并将相关的文件夹引入到项目中
- 在工程中添加所需头文件
- #import "KVNProgress.h"
二、关键属性及方法介绍
- @property (nonatomic, getter = isFullScreen) BOOL fullScreen;//设置是否全屏显示
- + (void)show; //显示提示框
- + (void)showWithStatus:(NSString *)status; // 在图形的下方添加描述状态的文字
- + (void)showProgress:(CGFloat)progress; // 用0-1表示进度加载的%0-%100
- + (void)showProgress:(CGFloat)progress status:(NSString*)status; // 在显示进度的图形下面自定义一段文字
- + (void)showSuccessWithStatus:(NSString *)status;//显示加载成功
- + (void)showErrorWithStatus:(NSString *)status;//显示加载失败
- + (void)dismiss;//移除提示框
- + (void)updateStatus:(NSString*)status; //根据进度更新图形下方的文字
- + (void)updateProgress:(CGFloat)progress animated:(BOOL)animated; //更新此时加载的进度
- + (void)setConfiguration:(KVNProgressConfiguration *)newConfiguration;//设置进度视图的样式
- + (instancetype)defaultConfiguration;//默认图形样式
三、使用场景1:基本使用
- 添加相关属性
- #import "ViewController.h"
- #import "KVNProgress.h"
- @interface ViewController ()
- //框架相关属性
- @property (nonatomic) KVNProgressConfiguration *basicConfiguration;//显示进度的图形为基本样式
- @property (nonatomic, strong) UIButton *basicBtn;//显示基本样式的按钮
- @end
- 使用懒加载方式初始化用到的控件
- - (UIButton *)basicBtn{
- if (!_basicBtn) {
- _basicBtn = [[UIButton alloc] initWithFrame:CGRectMake(119, 136, 137, 30)];
- [_basicBtn setTitle:@"basic" forState:UIControlStateNormal];
- _basicBtn.backgroundColor = [UIColor grayColor];
- [_basicBtn addTarget:self action:@selector(basic) forControlEvents:UIControlEventTouchUpInside];
- [self.view addSubview:_basicBtn];
- }
- return _basicBtn;
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- [self myWebView];
- [self basicBtn];
- //设置加载进度图形的样式为基本样式
- [KVNProgress setConfiguration:self.basicConfiguration];
- //设置基本样式为默认的样式
- self.basicConfiguration = [KVNProgressConfiguration defaultConfiguration];
- }
- 完成KVNProgress的相关设置
- - (void) basic{
- [KVNProgress show];
- //延时1秒以后移除提示框
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- self.basicConfiguration.tapBlock = nil;
- [KVNProgress dismiss];
- });
- }
四、使用场景2:加载成功与加载错误
当我们登陆成功或者登陆失败之后,经常出现一个相关的提示框,如下图:
- 添加相关属性
- @property (nonatomic, strong) UIButton *successBtn;
- @property (nonatomic, strong) UIButton *errorBtn;
- 使用懒加载方式初始化用到的控件
- - (UIButton *)successBtn{
- if (!_successBtn) {
- _successBtn = [[UIButton alloc] initWithFrame:CGRectMake(119, 241, 137, 30)];
- [_successBtn setTitle:@"success" forState:UIControlStateNormal];
- _successBtn.backgroundColor = [UIColor grayColor];
- [_successBtn addTarget:self action:@selector(success) forControlEvents:UIControlEventTouchUpInside];
- [self.view addSubview:_successBtn];
- }
- return _successBtn;
- }
- - (UIButton *)errorBtn{
- if (!_errorBtn) {
- _errorBtn = [[UIButton alloc] initWithFrame:CGRectMake(119, 341, 137, 30)];
- [_errorBtn setTitle:@"error" forState:UIControlStateNormal];
- _errorBtn.backgroundColor = [UIColor grayColor];
- [_errorBtn addTarget:self action:@selector(error) forControlEvents:UIControlEventTouchUpInside];
- [self.view addSubview:_errorBtn];
- }
- return _errorBtn;
- }
- 完成KVNProgress的相关设置
- - (void) success{
- [KVNProgress show];
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- self.basicConfiguration.tapBlock = nil;
- //提示加载成功
- [KVNProgress showSuccessWithStatus:@"Success"];
- });
- }
- - (void) error{
- [KVNProgress show];
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- self.basicConfiguration.tapBlock = nil;
- //提示加载失败
- [KVNProgress showErrorWithStatus:@"Error"];
- });
- }
五、使用场景3:复杂使用
在我们加载网页的时候,需要根据它的加载情况来改变提示框中的图形,如下图:
- 添加相关属性及其代理协议
- @interface ViewController ()
- @property (nonatomic, strong) UIButton *complexBtn;
- @property (nonatomic, strong) UIWebView *myWebView;
- @end
- 使用懒加载方式初始化用到的控件
- - (UIButton *)complexBtn{
- if (!_complexBtn) {
- _complexBtn = [[UIButton alloc] initWithFrame:CGRectMake(119, 441, 137, 30)];
- [_complexBtn setTitle:@"complex" forState:UIControlStateNormal];
- _complexBtn.backgroundColor = [UIColor grayColor];
- [_complexBtn addTarget:self action:@selector(complex) forControlEvents:UIControlEventTouchUpInside];
- [self.view addSubview:_complexBtn];
- }
- return _complexBtn;
- }
- - (UIWebView *)myWebView{
- if (!_myWebView) {
- _myWebView = [[UIWebView alloc] initWithFrame:self.view.bounds];
- NSURL *url = [NSURL URLWithString:@"http://www.hcios.com"];
- NSURLRequest *myRequest = [NSURLRequest requestWithURL:url];
- [_myWebView loadRequest:myRequest];
- _myWebView.delegate = self;
- [self.view addSubview:_myWebView];
- }
- return _myWebView;
- }
- 完成KVNProgress的相关设置
- - (void) complex{
- //创建一个视图控制器用来显示webView
- UIViewController *myController = [[UIViewController alloc] init];
- //创建一个返回原来控制器的按钮
- UIButton *backBtn = [[UIButton alloc] initWithFrame:CGRectMake(10,10, 50, 50)];
- [backBtn setTitle:@"返回" forState:UIControlStateNormal];
- backBtn.backgroundColor = [UIColor redColor];
- [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
- [self.myWebView addSubview:backBtn];
- [myController.view addSubview: self.myWebView];
- [self presentViewController:myController animated:YES completion:nil];
- }
- - (void) back{
- [self dismissViewControllerAnimated:YES completion:nil];
- }
- //在webView开始加载时候调用的函数里面设置相应的加载动画
- - (void)webViewDidStartLoad:(UIWebView *)webView{
- //开始加载,文字可以自定义
- [KVNProgress showProgress:0.0f
- status:@"加载中"];
- //延时0.2秒以后更新进度条显示加载到%50的位置
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- [KVNProgress updateProgress:0.5f
- animated:YES];
- });
- //延时0.5秒以后修改图形下方的文字
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- [KVNProgress updateStatus:@"请耐心等待"];
- });
- }
- //当webView加载完毕以后调用
- - (void)webViewDidFinishLoad:(UIWebView *)webView{
- //加载进度条显示加载完毕
- [KVNProgress updateProgress:1.0f
- animated:YES];
- //延时0.1秒以后隐藏加载进度的图形
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- [KVNProgress dismiss];
- });
- }