0
点赞
收藏
分享

微信扫一扫

UIWebView 进度条控件 NJKWebViewProgress的使用和分析


使用:(基本拷贝自以下github地址)
下载地址:[url]https://github.com/ninjinkun/NJKWebViewProgress[/url]

_progressProxy = [[NJKWebViewProgress alloc] init]; // instance variable  
webView.delegate = _progressProxy;
_progressProxy.webViewProxyDelegate = self;
_progressProxy.progressDelegate = self;



-(void)webViewProgress:(NJKWebViewProgress *)webViewProgress updateProgress:(float)progress  
{
[progressView setProgress:progress animated:NO];
}



progressProxy.progressBlock = ^(float progress) {  
[progressView setProgress:progress animated:NO];
};



分析


之前也有遇到需要做webview进度条的需求,但是一直没有好的方法。最后我的处理方法是使用NSURLRquest 去请求数据,请求的进度可以拿到,请求结束之后把请求的数据加载到webview。


这样请求完成之前是不会显示数据的,只显示了进度条。所以很好奇NJKWebViewProgress是怎么做到的,分析如下:


[list]


[*]webViewDidStartLoad 是一个请求的开始,所有的请求都要经过它,未加载资源之前,能够得到一个URL 有多少个资源需要加载,使用_loadingCount++ 方式来计数。


[*]webViewDidFinishLoad、didFailLoadWithError 是一个请求的结束,每次请求结束 _loadingCount --,并重新计数进度


[*]进度使用 _loadingCount/_maxLoadCount 的方式来计算


[*]每次webViewDidFinishLoad、didFailLoadWithError 请求都加入了 waitForCompleteJS 这样的js到web view中,来检测网页是否加载完成。


[*]把得到进度逻辑和展示进度的视图分开写,用代理把两个类联系起来,结构清晰、实现起来也会方便很多


[/list]


总结



举报

相关推荐

0 条评论