How to find out whether NSScrollView is currently scrolling
Asked Answered
P

3

9

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!

Peonage answered 16/12, 2012 at 13:0 Comment(0)
P
14

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];
}
Pornography answered 16/12, 2012 at 16:2 Comment(0)
M
7

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)
Mud answered 19/5, 2016 at 17:22 Comment(1)
Why is the selector scrollViewDidScroll in the past tense, but the notification name willStartLiveScrollNotification in the future tense?Conaway
U
2

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
    }
Unforgettable answered 10/4, 2019 at 7:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.