How to observe NSScroller changes?

I have a subclass of NSScrollView , and I would like to update another NSView based on the current scroll position. I tried the KVC observation value [self horizontalScroller] but never received the call.

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

Do you see an error in my reasoning or do you know the best way to observe the scrolling of an NSScrollView ?

+4
source share
1 answer

Tell the scroll by viewing the contents of the content to post changes to the changed notifications, then listen to NSViewBoundsDidChangeNotification.

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

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

 NSPoint currentScrollPosition = [[theScrollView contentView] bounds].origin; 
+6
source

Source: https://habr.com/ru/post/1307720/


All Articles