一削饵、實(shí)現(xiàn):
myViewController.h :
@interface myViewController : UIViewController {
BOOL theBool;
//IBOutlet means you can place the progressView in Interface Builder and connect it to your code
IBOutlet UIProgressView* myProgressView;
NSTimer *myTimer;
}
@end
myViewController.m:
#import "myViewController.h"
@implementation myViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 仿微信進(jìn)度條
CGFloat progressBarHeight = 2.f;
CGRect navigationBarBounds = self.navigationController.navigationBar.bounds;
CGRect barFrame = CGRectMake(0, navigationBarBounds.size.height - progressBarHeight, navigationBarBounds.size.width, progressBarHeight);
myProgressView = [[UIProgressView alloc] initWithFrame:barFrame];
myProgressView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
myProgressView.progressTintColor = [UIColor colorWithRed:43.0/255.0 green:186.0/255.0 blue:0.0/255.0 alpha:1.0];
[self.navigationController.navigationBar addSubview:myProgressView];
// 設(shè)置網(wǎng)絡(luò)請(qǐng)求失敗情況頁(yè)面顯示
[self loadFailViewSetting];
// 請(qǐng)求網(wǎng)絡(luò)
[self reRequesrtUrl];
}
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
// 移除 progress view
// because UINavigationBar is shared with other ViewControllers
[myProgressView removeFromSuperview];
}
- (void)webViewDidStartLoad:(UIWebView *)webView{
myProgressView.progress = 0;
theBool = false;
//0.01667 is roughly 1/60, so it will update at 60 FPS
myTimer = [NSTimer scheduledTimerWithTimeInterval:0.01667 target:self selector:@selector(timerCallback) userInfo:nil repeats:YES];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
theBool = true;
}
-(void)timerCallback {
if (theBool) {
if (myProgressView.progress >= 1) {
myProgressView.hidden = true;
[myTimer invalidate];
myTimer = nil;
}
else {
myProgressView.progress += 0.1;
}
}
else {
myProgressView.progress += 0.05;
if (myProgressView.progress >= 0.95) {
myProgressView.progress = 0.95;
// 注:這里關(guān)閉時(shí)間計(jì)時(shí)器,不然如果網(wǎng)頁(yè)加載非常慢券腔,計(jì)時(shí)器會(huì)一直執(zhí)行胎源,因?yàn)樗⑿卤容^迅速,會(huì)導(dǎo)致頁(yè)面卡頓吉执。這里關(guān)閉,在網(wǎng)頁(yè)請(qǐng)求結(jié)束代理里面地来,再執(zhí)行隱藏進(jìn)度條動(dòng)畫
[myTimer invalidate];
myTimer = nil;
}
}
}
@end
二戳玫、原理:
It's difficult (if even possible), since you would have to track all resources loaded by the site, but …
I have one idea. It's more of a **trick** than a real solution, but I think even Apple uses this in Messages app :)
1. When you start loading the page, **begin an animation to 90%** of the progress (let's say with duration of 1.5 seconds, maybe be different for Wi-Fi, LTE, 3G, …).
2. When page loads in meantime, **quickly animate the progress to 100%**. Done!
3. If the page takes more time to load, the progress bar **will stop at 90% and will wait there**. Frustrating moment when the user watches slow spinning indicator in status bar! And then finally, the page finish loading and (as in bullet 2.) you play **quick animation to 100%**. Done!
I think we all know, that this is how Messages app works, since I don't believe sending SMS can be tracked with such accurate progress :)
大致意思,就是這個(gè)進(jìn)度條是個(gè)假象未斑,先進(jìn)度到90%咕宿,然后等待加載完畢,完畢后瞬間進(jìn)度到100%蜡秽。
仿微信進(jìn)度條府阀,暫時(shí)是這樣處理的。如您有更好的方法载城,歡迎給予指點(diǎn)肌似。
三、補(bǔ)充(2016.11.02):
iOS 8 之后可以使用WKWebView替換UIWebView,性能各方面有很大提高诉瓦。學(xué)習(xí)期間寫了個(gè)小demo,點(diǎn)擊下載 下載地址 。
參考鏈接: