计算wkWebView进度条
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (object == self.webview && [keyPath isEqualToString:@"estimatedProgress"]) {
CGFloat newprogress = [[change objectForKey:NSKeyValueChangeNewKey] doubleValue];
if (newprogress == 1) {
self.progressView.hidden = YES;
[self.progressView setProgress:0 animated:NO];
}else {
self.progressView.hidden = NO;
[self.progressView setProgress:newprogress animated:YES];
}
}
}
关于使用kvo监听模式 change 取值问题
使用CGRect newRect = [[change objectForKey:NSKeyValueChangeNewKey] CGRectValue];
boolValue、integerValue、intValue、CGPointValue等等 取到你想要的数据
不能使用valueForKey取值
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void *)context{
if ([keyPath isEqualToString:@"frame"]) {
CGRect newRect = [[change objectForKey:NSKeyValueChangeNewKey] CGRectValue];
if (newRect.size.width == ScreenW) {
//扩散到整个屏幕
}
}
}
网友评论