Question

How can I find out whether my NSScrollView is currently scrolling? On iOS I can use the delegate but despite of googling a lot I can't find a way to do this on the Mac.

Thanks in advance!

Was it helpful?

Solution

You can receive notification NSViewBoundsDidChangeNotification like shown below

NSView *contentView = [scrollview contentView];

[contentView setPostsBoundsChangedNotifications:YES];

// a register for those notifications on the content view.
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(boundDidChange:)
                                             name:NSViewBoundsDidChangeNotification
                                           object:contentView];

The notification method

- (void)boundDidChange:(NSNotification *)notification {
    // get the changed content view from the notification
    NSClipView *changedContentView=[notification object];
}

OTHER TIPS

As of OS X 10.9 there is also NSScrollView.willStartLiveScrollNotification. It's posted on the main thread at the beginning of a user scroll event.

E.g.

NotificationCenter.default.addObserver(self, selector: #selector(scrollViewDidScroll), name: NSScrollView.willStartLiveScrollNotification, object: nil)

my two cents for OSX mojave / swift 5

 let nc =  NotificationCenter.default

    nc.addObserver(
        self,
        selector: #selector(scrollViewWillStartLiveScroll(notification:)),
        name: NSScrollView.willStartLiveScrollNotification,
        object: scrollView
    )

    nc.addObserver(
        self,
        selector: #selector(scrollViewDidEndLiveScroll(notification:)),
        name: NSScrollView.didEndLiveScrollNotification,
        object: scrollView
    )

....

   @objc func scrollViewWillStartLiveScroll(notification: Notification){
        #if DEBUG
        print("\(#function) ")
        #endif
    }


    @objc func scrollViewDidEndLiveScroll(notification: Notification){
        #if DEBUG
        print("\(#function) ")
        #endif
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top