使用kvo监听浏览器的进度条以及kvo取值的基本使用方法

2017-11-28  本文已影响21人  Sharb

计算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) {

//扩散到整个屏幕

}

}

}

上一篇下一篇

猜你喜欢

热点阅读