如何观察NSScroller的变化? [英] How to observe NSScroller changes?

查看:168
本文介绍了如何观察NSScroller的变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 NSScrollView 子类,我想根据当前的滚动位置更新另一个 NSView 。我尝试KVC-观察值的 [self horizo​​ntalScroller] 但是从来没有被调用。

I have an NSScrollView subclass and I would like to update another NSView based on the current scroll position. I tried KVC-observing the value of [self horizontalScroller] but that never gets called.

// 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
    }
}

您看到我的推理错误或知道更好的方法如何观察 NSScrollview的滚动 c>

Do you see an error in my reasoning or know a better method of how to observe the scrolling of an NSScrollview?

推荐答案

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

Scroll View Programming Guide ,您可以获取当前滚动这样定位:

As stated in the Scroll View Programming Guide, you can get the current scroll position this way:

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

这篇关于如何观察NSScroller的变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆