QVO - È=10=È 误报?
KVO - UIScrollView.contentSize false positive?
使用 WKWebview,我正在尝试监视对 webView.scrollView.contentSize
的更改。问题是我收到了相同大小的多个通知。每当我滚动时,都会发送通知,即使 contentSize 保持不变。我知道我可以通过在我的处理程序中跟踪以前的大小来自己解决这个问题,但是我对 KVO 在引擎盖下的工作方式并没有真正的信心,并且担心这可能会很昂贵。
let handler = {(scrollView: UIScrollView, change: NSKeyValueObservedChange<CGSize>) in
if let contentSize = change.newValue {
print ("ContentSize", contentSize)
}
}
obs.insert(webView.scrollView.observe(\UIScrollView.contentSize, options: [NSKeyValueObservingOptions.new], changeHandler: handler))
输出:(由滚动生成)
ContentSize (1366.0, 2061.0)
ContentSize (1366.0, 2061.0)
ContentSize (1366.0, 2061.0)
ContentSize (1366.0, 2061.0)
我不明白为什么会看到此通知,因为 contentSize 不是新值。我误会了什么吗?我应该自己存储 prevSize 并在处理程序中检查更改吗?
UIKit 中的 classes 通常不支持 KVO,如下所述:
"Although the classes of the UIKit framework generally do not support KVO, you can still implement it in the custom objects of your application, including custom views."
除非 class 被记录为支持 KVO,否则您应该假设它不支持(即使它看起来有效)。它可能在 iOS 的一个版本中有效,但在下一个版本...
中无效
使用 WKWebview,我正在尝试监视对 webView.scrollView.contentSize
的更改。问题是我收到了相同大小的多个通知。每当我滚动时,都会发送通知,即使 contentSize 保持不变。我知道我可以通过在我的处理程序中跟踪以前的大小来自己解决这个问题,但是我对 KVO 在引擎盖下的工作方式并没有真正的信心,并且担心这可能会很昂贵。
let handler = {(scrollView: UIScrollView, change: NSKeyValueObservedChange<CGSize>) in
if let contentSize = change.newValue {
print ("ContentSize", contentSize)
}
}
obs.insert(webView.scrollView.observe(\UIScrollView.contentSize, options: [NSKeyValueObservingOptions.new], changeHandler: handler))
输出:(由滚动生成)
ContentSize (1366.0, 2061.0)
ContentSize (1366.0, 2061.0)
ContentSize (1366.0, 2061.0)
ContentSize (1366.0, 2061.0)
我不明白为什么会看到此通知,因为 contentSize 不是新值。我误会了什么吗?我应该自己存储 prevSize 并在处理程序中检查更改吗?
UIKit 中的 classes 通常不支持 KVO,如下所述:
"Although the classes of the UIKit framework generally do not support KVO, you can still implement it in the custom objects of your application, including custom views."
除非 class 被记录为支持 KVO,否则您应该假设它不支持(即使它看起来有效)。它可能在 iOS 的一个版本中有效,但在下一个版本...
中无效