Callbacks When an NSScrollView is Scrolled?

2020-02-07 17:37发布

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.

5条回答
Viruses.
2楼-- · 2020-02-07 17:58

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)
查看更多
Summer. ? 凉城
3楼-- · 2020-02-07 17:59

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){

    }
查看更多
等我变得足够好
4楼-- · 2020-02-07 18:19

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]; 
查看更多
叛逆
5楼-- · 2020-02-07 18:20

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.

查看更多
Bombasti
6楼-- · 2020-02-07 18:22

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

查看更多
登录 后发表回答