NJKWebViewProgress是一個(gè)能使UIWebview顯示加載進(jìn)度的第三方控件。在網(wǎng)上以“UIWebview+進(jìn)度”為關(guān)鍵字搜索,NJKWebViewProgress是前幾名的解決方法。
以下分析該控件的實(shí)現(xiàn)方式。

使用方式
@property(weak,nonatomic) UIWebView *uiwebView;
@property(strong,nonatomic,readwrite) NJKWebViewProgressView *progressView;
@property(strong,nonatomic,readwrite) NJKWebViewProgress *progressProxy;
- (void)configProgress {
self.progressProxy= [[NJKWebViewProgressalloc]init];
self.uiwebView.delegate=self.progressProxy;
self.progressProxy.webViewProxyDelegate=self;
self.progressProxy.progressDelegate=self;
CGRect barFrame = ...;
self.progressView= [[NJKWebViewProgressViewalloc]initWithFrame:barFrame];
[destView addSubview:self.progressView];
}
#pragma mark - NJKWebViewProgressDelegate
-(void)webViewProgress:(NJKWebViewProgress*)webViewProgress updateProgress:(float)progress
{
[self.progressView setProgress:progress animated:YES];
}
使用方法很簡(jiǎn)單,大意是
設(shè)置UIWebVIew的delegate為progressProxy
設(shè)置progressProxy.webViewProxyDelegate為self
設(shè)置progressProxy.progressDelegate為self
將progressView添加到需要的位置
通過(guò)progressProxy.progressDelegate的實(shí)現(xiàn)方法,在self中修改progressView的progress值
實(shí)現(xiàn)分析
從上文的使用方法中,已經(jīng)能看出progressProxy提供progress值,progressView顯示progress值,所以只需分析progressProxy如何得到進(jìn)度即可。
@property(nonatomic, njk_weak) id<UIWebViewDelegate> webViewProxyDelegate;
可以發(fā)現(xiàn),使用的仍然是UIWebView的代理方法。而UIWebView本身是沒(méi)有提供進(jìn)度查詢方法的,所以NJKWebViewProgress在這里又是怎么解決這個(gè)問(wèn)題的呢?
const float NJKInitialProgressValue =0.1f;
const float NJKInteractiveProgressValue =0.5f;
const float NJKFinalProgressValue =0.9f;

分析代碼以后,發(fā)現(xiàn)其中的實(shí)現(xiàn)大致如上圖所示。按照具體情況將進(jìn)度設(shè)置為
0->0.1->(0.5->)0.9->1
是一個(gè)根據(jù)狀態(tài)來(lái)決定當(dāng)前進(jìn)度的實(shí)現(xiàn)思路。

總結(jié)
雖然NJKWebViewProgress從本質(zhì)來(lái)說(shuō)是一個(gè)假進(jìn)度條,但是它的顯示效果確實(shí)不錯(cuò)。使用方便,擴(kuò)展性強(qiáng),而且能使用cocoaPods管理,是一個(gè)非常不錯(cuò)的控件。
以上僅是個(gè)人理解,如果有謬誤,請(qǐng)指正!
00000003