最近項(xiàng)目把UIWebView替換成WKWebView,其他的都還挺順利基本上資料還都挺全的,不過發(fā)現(xiàn)一個(gè)問題:當(dāng)實(shí)現(xiàn)webView的ScrollowView的
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
代理的時(shí)候,發(fā)現(xiàn)網(wǎng)頁滑動(dòng)的時(shí)候屏幕之外的地方都是白屏閃現(xiàn),感覺渲染出現(xiàn)問題,就去網(wǎng)上查了好久,發(fā)現(xiàn)這個(gè)問題的資料好少。最后看到一篇文章分析的挺好
http://www.itdecent.cn/p/1d739e2e7ed2
感覺問題相似就想直接使用他的這個(gè)方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
if ([[self respondsToSelector:@selector(_updateVisibleContentRects)]) {
((void(*)(id,SEL,BOOL))objc_msgSend)([self,@selector(_updateVisibleContentRects),NO);
}
#pragma clang diagnostic pop
..........
}
但是還是怕使用私有API被拒。
也想到直接使用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
[self setNeedsLayout];
..........
}
但是發(fā)現(xiàn)使用之后webView的上拉下拉效果失效,也怕影響性能
最后還是多改幾行代碼使用KVO監(jiān)控scrollView(明明打印的self.scrollView.bounces一直是YES,有時(shí)間研究下webView的彈簧效果為啥失效)
NSString *const TPKeyPathContentOffset = @"contentOffset";
NSKeyValueObservingOptions options = NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld;
[self.scrollView addObserver:self forKeyPath:TPKeyPathContentOffset options:options context:nil];
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:TPKeyPathContentOffset]) {
}
}
}
-(void)dealloc
{
[self.scrollView removeObserver:self forKeyPath:TPKeyPathContentOffset];
}
由于我粘貼的代碼是項(xiàng)目中WKWebView的封裝,本文中self都是指WKWebView