• iOS UIWebView 之 UIProgressView


    之前做等待跳转都是用UIActivityIndicatorView ,后来做webView 加载页面的时候,发现了一个特别好用又超级炫酷的加载提示NJKWebViewProgress,作者巧妙的通过计算加载请求的个数,用百分比展示进度,并且集成了加载页面的title,效果惊艳业界。今将使用步骤记录,方便日后查询,也希望能给朋友们带来一点帮助。

    1、首先将炫酷的NJKWebViewProgress 拖到工程,赶紧"占为已有"

    2、在加载页面铺以下代码,由于对NJKWebViewProgress的理解都是个人的,就不一一注释了,以免不到之处误导查询者。

    @interface ViewController ()<UIWebViewDelegate,NJKWebViewProgressDelegate>{

        UIWebView *_webView;
        NJKWebViewProgressView *_progressView;
        NJKWebViewProgress *_progressProxy;
        NSURLRequest *req;
        
        BOOL cameraBool;
        UIImagePickerController *cameraPick;
        
        
    }
    @end

    @implementation ViewController

    - (void)viewDidLoad {
        [super viewDidLoad];
        _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0,WIDTH, HEIGHT)];
        _webView.scalesPageToFit = YES;
        [self.view addSubview:_webView];
        
        
        UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self action:@selector(replyEvent)];
        UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refreshEvent)];
        
        self.navigationItem.leftBarButtonItem = item;
        self.navigationItem.rightBarButtonItem = item1;

        _progressProxy = [[NJKWebViewProgress alloc] init];
        _webView.delegate = _progressProxy;
        _progressProxy.webViewProxyDelegate = self;
        _progressProxy.progressDelegate = self;
        
        CGFloat progressBarHeight = 2.f;
        CGRect navigationBarBounds = self.navigationController.navigationBar.bounds;
        CGRect barFrame = CGRectMake(0, navigationBarBounds.size.height - progressBarHeight, navigationBarBounds.size.width, progressBarHeight);
        _progressView = [[NJKWebViewProgressView alloc] initWithFrame:barFrame];
        _progressView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;

        [self jump];
    }

    - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
        [self.navigationController.navigationBar addSubview:_progressView];
    }

    -(void)viewWillDisappear:(BOOL)animated
    {
        [super viewWillDisappear:animated];
        [_progressView removeFromSuperview];
    }


    -(void)replyEvent{
        
        [_webView goBack];
    }

    -(void)refreshEvent{
        [_webView reload];
    }

    -(void)jump{
        
        if(self.k == 1){
            req = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://www.baidu.com/index.php?tn=monline_3_dg"]];}
    //    }else if (self.k == 2){
    //    req = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.cnblogs.com/wangyang1213/"]];
    //    }else if(self.k == 3){
    //       req = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.163.com/"]];
    //    }
        else{
        req = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://cc.yztcai.com/h5.php"]];
        }
        
        [_webView loadRequest:req];
    }

    - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
        
        if(error != NULL){
            UIAlertController *alertController1 = [UIAlertController alertControllerWithTitle:@"提示" message:@"网络连接异常" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *sureAction1 = [UIAlertAction actionWithTitle:@"确定" style:0 handler:nil];
            UIAlertAction *cancelAction1 = [UIAlertAction actionWithTitle:@"取消" style:0 handler:nil];
            [alertController1 addAction:sureAction1];
            [alertController1 addAction:cancelAction1];
            [self presentViewController:alertController1 animated:YES completion:nil];
        }
    }

    -(void)webViewProgress:(NJKWebViewProgress *)webViewProgress updateProgress:(float)progress
    {
        [_progressView setProgress:progress animated:YES];
        self.title = [_webView stringByEvaluatingJavaScriptFromString:@"document.title"];
    }

    至此便集成了NJKWebViewProgress。拖到工程试试。

  • 相关阅读:
    Scrapy 概览笔记
    Python 依赖版本控制 (requirements.txt 文件生成和使用)
    Python 虚拟空间的使用
    macOS 所有版本 JDK 安装指南 (with Homebrew)
    鉴权那些事
    Java 位运算符和 int 类型的实现
    ASP.NET Core 入门教程 5、ASP.NET Core MVC 视图传值入门
    如何做好一次知识或技术分享
    ASP.NET Core 入门教程 4、ASP.NET Core MVC控制器入门
    ASP.NET Core 入门教程 3、ASP.NET Core MVC路由入门
  • 原文地址:https://www.cnblogs.com/wangyang1213/p/5286101.html
Copyright © 2020-2023  润新知