Commit 6b473c0d122bea4b0e81f4f7c31f73559be595cd
1 parent
a5ffeb79
提交添加web进度条
Showing
2 changed files
with
37 additions
and
2 deletions
CNLiveServiceCenterModule.podspec
CNLiveServiceCenterModule/Classes/CNLiveServiceCenterModule/Controller/CNLiveBCreatorViewController.m
| ... | ... | @@ -30,6 +30,9 @@ |
| 30 | 30 | |
| 31 | 31 | @property (strong, nonatomic) NSString *url; |
| 32 | 32 | |
| 33 | +@property (assign,nonatomic) NSUInteger loadCount; | |
| 34 | +@property (assign,nonatomic) UIProgressView *progressView; | |
| 35 | + | |
| 33 | 36 | @end |
| 34 | 37 | |
| 35 | 38 | @implementation CNLiveBCreatorViewController |
| ... | ... | @@ -46,14 +49,46 @@ |
| 46 | 49 | |
| 47 | 50 | -(void)initWKWebView |
| 48 | 51 | { |
| 52 | + | |
| 53 | + UIProgressView *progress = [[UIProgressView alloc]initWithFrame:CGRectMake(0, kNavigationBarHeight, KScreenWidth, 1)]; | |
| 54 | + progress.tintColor = RGBOF(0x389FEF); | |
| 55 | + progress.trackTintColor = [UIColor whiteColor]; | |
| 56 | + [self.view addSubview:progress]; | |
| 57 | + self.progressView = progress; | |
| 58 | + | |
| 59 | + | |
| 49 | 60 | self.view.backgroundColor = [UIColor whiteColor]; |
| 50 | 61 | self.url = CNLiveBCenterCreaterUrl; |
| 51 | 62 | WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, KScreenHeight)]; |
| 52 | - self.view = webView; | |
| 63 | + [self.view addSubview:webView]; | |
| 64 | + webView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight; | |
| 65 | + [webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil]; | |
| 53 | 66 | NSURL *url = [NSURL URLWithString:self.url]; |
| 54 | 67 | NSURLRequest *request = [NSURLRequest requestWithURL:url]; |
| 55 | 68 | [webView loadRequest:request]; |
| 69 | + | |
| 70 | + [self.view insertSubview:webView belowSubview:progress]; | |
| 71 | + | |
| 72 | +} | |
| 56 | 73 | |
| 74 | +- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{ | |
| 75 | + | |
| 76 | + if ([keyPath isEqualToString:@"estimatedProgress"]) { | |
| 77 | + CGFloat newProgress = [[change objectForKey:NSKeyValueChangeNewKey] doubleValue]; | |
| 78 | + if (newProgress == 1) { | |
| 79 | + self.progressView.hidden = YES; | |
| 80 | + [self.progressView setProgress:0 animated:NO]; | |
| 81 | + }else{ | |
| 82 | + self.progressView.hidden = NO; | |
| 83 | + [self.progressView setProgress:newProgress animated:YES]; | |
| 84 | + } | |
| 85 | + } | |
| 86 | +} | |
| 87 | + | |
| 88 | +- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{ | |
| 89 | + // 在发送请求之前,决定是否跳转 | |
| 90 | + decisionHandler(WKNavigationActionPolicyAllow); | |
| 91 | + | |
| 57 | 92 | |
| 58 | 93 | } |
| 59 | 94 | ... | ... |