48

I'm making a Mac app which needs to know when the user is scrolling the NSScrollView, however, I can't find any methods like UIScrollView, which has the following delegate methods:

– scrollViewDidScroll:
– scrollViewWillBeginDragging:
– scrollViewDidEndDragging:willDecelerate:
– scrollViewShouldScrollToTop:
– scrollViewDidScrollToTop:
– scrollViewWillBeginDecelerating:
– scrollViewDidEndDecelerating:

Can I have the similar delegate methods for the App Kit? Thanks in advance.

Kai.

nonamelive
  • 6,510
  • 8
  • 40
  • 47

6 Answers6

79

You can monitor a scroll view's changes by monitoring the bounds of it's content view. First set the content view to post its changes with

[contentView setPostsBoundsChangedNotifications:YES];

Then register as an observer of those notifications with

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(boundsDidChange:) name:NSViewBoundsDidChangeNotification object:contentView]; 
Sean Rich
  • 2,338
  • 2
  • 20
  • 15
10

Update for Swift 4:

scrollView.contentView.postsBoundsChangedNotifications

Also the call is:

NotificationCenter.default.addObserver(self,
                                       selector: #selector(boundsChange),
                                       name: NSView.boundsDidChangeNotification,
                                       object: scrollView.contentView)

Edit: the collection in mac doesn't inherit from scrollview. updated properly

Pavel Alexeev
  • 6,026
  • 4
  • 43
  • 51
wolffan
  • 1,084
  • 10
  • 15
4

Had the same problem recently... To somewhat emulate deceleration callbacks it is possible to override

-(void) scrollWheel:(NSEvent *)theEvent 

of NSScrollView class, but then check theEvent.momentumPhase instead of theEvent.phase for event phases.

4

Adding to @Sean Rich answer.

The contentView is the NSClipView between the NSScrollView and NSCollectionView.

Picture of clip view from the Storyboard

For this to work, both the ClipView needs to be set to postsBoundsChangedNotifications and should be passed in the notification object.

self.clipView.postsBoundsChangedNotifications = true

NotificationCenter.default.addObserver(self,
                                       selector: #selector(collectionViewDidScroll(notification:)),
                                       name: NSView.boundsDidChangeNotification,
                                       object: self.clipView)
bauerMusic
  • 5,470
  • 5
  • 38
  • 53
  • Should there also be a subsequent removeObserver call? when the class is deInit ? – Sentry.co Nov 02 '20 at 15:02
  • @eonist Good question. You do not need to remove observer in this call. From Apple: "If your app targets iOS 9.0 and later or macOS 10.11 and later, you do not need to unregister an observer that you created with this function" https://developer.apple.com/documentation/foundation/notificationcenter/1415360-addobserver – bauerMusic Nov 02 '20 at 17:39
  • thanks, almost like a callback then. You don't have to deregister them either – Sentry.co Nov 03 '20 at 09:53
0

my two cents for swift 4.2 OSX:

....

if let clipView = self.collectionView.superview, let sv = clipView.superview as? NSScrollView{

        let contentView = sv.contentView
        contentView.postsBoundsChangedNotifications = true

        NotificationCenter.default.addObserver(self,
                                               selector: #selector(collectionViewDidScroll(notification:)),
                                               name: NSView.boundsDidChangeNotification,
                                               object: clipView)
}








 //MARK: scrollview observer:

    @objc func collectionViewDidScroll(notification: Notification){

    }
ingconti
  • 10,876
  • 3
  • 61
  • 48
0

Use the following:

NSScrollViewWillStartLiveScrollNotification
NSScrollViewDidLiveScrollNotification
NSScrollViewDidEndLiveScrollNotification

NSNotificationCenter.defaultCenter().addObserver(
    self,
    selector: #selector(scrollViewDidScroll(_:)),
    name: NSScrollViewDidLiveScrollNotification,
    object: scrollView
)
Cal
  • 359
  • 3
  • 5