• NJKWebViewProgress ——webview进度条


    导入头文件
    #import "NJKWebViewProgressView.h"
    #import "NJKWebViewProgress.h"
    遵守协议
      <UIWebViewDelegate, NJKWebViewProgressDelegate>
    实现代码
    @implementation ViewController
    {
        IBOutlet __weak UIWebView *_webView;
        NJKWebViewProgressView *_webViewProgressView;
        NJKWebViewProgress *_webViewProgress;
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        _webViewProgress = [[NJKWebViewProgress alloc] init];
        _webView.delegate = _webViewProgress;
        _webViewProgress.webViewProxyDelegate = self;
        _webViewProgress.progressDelegate = self;
    
    
    CGRect navBounds = self.navigationController.navigationBar.bounds;
    CGRect barFrame = CGRectMake(0,
                                 navBounds.size.height - 2,
                                 navBounds.size.width,
                                 2);
    _webViewProgressView = [[NJKWebViewProgressView alloc] initWithFrame:barFrame];
    _webViewProgressView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    [_webViewProgressView setProgress:0 animated:YES];
    [self loadBaidu];
    [self.navigationController.navigationBar addSubview:_webViewProgressView];
    }
    
    -(void)loadBidu
    {
        NSURLRequest *req = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.baidu.com/"]];
        [_webView loadRequest:req];
    }
    
    #pragma mark - NJKWebViewProgressDelegate
    -(void)webViewProgress:(NJKWebViewProgress *)webViewProgress updateProgress:(float)progress
    {
        [_webViewProgressView setProgress:progress animated:YES];
        self.title = [_webView stringByEvaluatingJavaScriptFromString:@"document.title"];
    }

    NJKWebViewProgress是怎么做到的,分析如下:

    • webViewDidStartLoad 是一个请求的开始,所有的请求都要经过它,未加载资源之前,能够得到一个URL 有多少个资源需要加载,使用_loadingCount++ 方式来计数。
    • webViewDidFinishLoaddidFailLoadWithError 是一个请求的结束,每次请求结束 _loadingCount --,并重新计数进度
    • 进度使用 _loadingCount/_maxLoadCount 的方式来计算
    • 每次webViewDidFinishLoaddidFailLoadWithError 请求都加入了 waitForCompleteJS 这样的js到web view中,来检测网页是否加载完成。
      • 把得到进度逻辑和展示进度的视图分开写,用代理把两个类联系起来,结构清晰、实现起来也会方便很多
  • 相关阅读:
    vue.js插入dom节点的方式
    window.atob()与window.btoa()方法实现编码与解码
    使用FileReader接口读取文件内容
    移动前端—H5实现图片先压缩再上传
    JS单体内置对象之Math常用方法(min,max,ceil,floor,round,random等)
    JS中使用document.defaultView.getComputedStyle()、currentStyle()方法获取CSS属性值
    JS按位非(~)运算符与~~运算符的理解分析
    JS获取键盘按下的键值event.keyCode,event.charCode,event.which的兼容性
    Bootstrap 按钮
    Bootstrap CSS 表单
  • 原文地址:https://www.cnblogs.com/sungk/p/5075832.html
Copyright © 2020-2023  润新知