icant
icant

Reputation: 888

How to find out whether NSScrollView is currently scrolling

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!

Upvotes: 9

Views: 4246

Answers (3)

ingconti
ingconti

Reputation: 11666

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
    }

Upvotes: 2

Tom Hamming
Tom Hamming

Reputation: 10991

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)

Upvotes: 7

dafi
dafi

Reputation: 3532

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];
}

Upvotes: 14

Related Questions