Swift NSScrollView 更好的 NSViewBoundsDidChangeNotification 观察? [英] Swift NSScrollView better NSViewBoundsDidChangeNotification observing?
问题描述
有没有更好的方法来观察 NSScrollView -> NSClipView 框架/边界变化?这是翻译成 Swift 的 Obj-C 代码:
Is there a better way to observe NSScrollView -> NSClipView frame/bounds change? This is Obj-C code translated to Swift:
public override init(frame frameRect: NSRect) {
self.contentView.postsFrameChangedNotifications = true;
NSNotificationCenter.defaultCenter().addObserver(
self,
selector: "frameDidChange:",
name: NSViewFrameDidChangeNotification,
object: self.contentView)
}
deinit {
NSNotificationCenter.defaultCenter().removeObserver(
self,
name: NSViewFrameDidChangeNotification,
object: self.contentView)
}
它有效,但我不想使用 NSNotificationCenter.我想在 NSClipView 上使用框架属性观察:
It works, but I don't want to use NSNotificationCenter. I want to use frame property observation on NSClipView:
class MyClipView: NSClipView {
...
override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
self.postsFrameChangedNotifications = true;
}
override var frame: CGRect {
didSet {
println("Did set frame")
}
}
}
但是 didSet 在初始化后只被调用了 2 次,并且在滚动封闭 NSScrollView 时它永远不会被调用.看起来苹果是使用内部方法来改变框架而不调用框架属性?
But didSet is called only 2 times after initialisation and it's never called when enclosing NSScrollView is scrolled. Looks that Apple is using internal methods to change the frame without calling frame property?
同样代表 NSViewBoundsDidChangeNotification 和 bounds 属性.
The same stands for NSViewBoundsDidChangeNotification and bounds property.
推荐答案
并非所有对 frame 变量的更改都必须通过属性发生:
==> 因此 didSet 没有捕获"所有
not every change to the frame variable has to happen through the property:
==> therefore the didSet doesn't 'catch' all
方法旁边的setFrame
(框架的setter)
还有其他方法:setFrameOrigin
和 setFrameSize
甚至 setFrameRotation
next to the method setFrame
(the setter for the frame)
there are also other ways: setFrameOrigin
and setFrameSize
and even setFrameRotation
边界相同
这篇关于Swift NSScrollView 更好的 NSViewBoundsDidChangeNotification 观察?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!