如何观察NSScroller的变化?

时间:2010-04-22 19:13:10

标签: cocoa key-value-observing nsscrollview

我有一个NSScrollView子类,我想根据当前滚动位置更新另一个NSView。我试过KVC观察value的{​​{1}}但是从未被调用过。

[self horizontalScroller]

您是否在我的推理中看到错误,或者知道如何观察// In awakeFromNib [[self horizontalScroller] addObserver:self forKeyPath:@"value" options:NSKeyValueObservingOptionNew context:NULL]; // Later in the file - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if (object == [self horizontalScroller] && [keyPath isEqualToString:@"value"]) { // This never gets called } } 滚动的更好方法?

1 个答案:

答案 0 :(得分:7)

告诉滚动视图的内容视图发布已更改的通知,然后侦听NSViewBoundsDidChangeNotification。

[[aScrollView contentView] setPostsBoundsChangedNotifications:YES];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(boundsDidChangeNotification:)
                                             name:NSViewBoundsDidChangeNotification
                                           object:[scrollView contentView]];

Scroll View Programming Guide中所述,您可以通过以下方式获取当前滚动位置:

NSPoint currentScrollPosition = [[theScrollView contentView] bounds].origin;