一、实现:
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
1
2
3
4
5
6
7
8
myViewController.m:
#import "myViewController.h"
@implementation myViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 仿微信进度条
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];
// 设置网络请求失败情况页面显示
[self loadFailViewSetting];
// 请求网络
[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];
}
else {
myProgressView.progress += 0.1;
}
}
else {
myProgressView.progress += 0.05;
if (myProgressView.progress >= 0.95) {
myProgressView.progress = 0.95;
}
}
}
@end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
二、原理:
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 bulle
————————————————
版权声明:本文为CSDN博主「北冥小鱼儿」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/leochang130731/article/details/50593507