44

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.

4

5 回答 5

77

您可以通过监视其内容视图的边界来监视滚动视图的更改。首先设置内容视图以发布其更改

[contentView setPostsBoundsChangedNotifications:YES];

然后注册为这些通知的观察者

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(boundsDidChange:) name:NSViewBoundsDidChangeNotification object:contentView]; 
于 2011-03-02T15:34:29.567 回答
6

Swift 4 的更新:

scrollView.contentView.postsBoundsChangedNotifications

电话也是:

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

编辑:mac 中的集合不继承自滚动视图。正确更新

于 2018-03-22T22:14:13.303 回答
4

最近有同样的问题......为了在某种程度上模拟减速回调,可以覆盖

-(void) scrollWheel:(NSEvent *)theEvent 

NSScrollView 类,但随后检查事件阶段的Event.momentumPhase而不是 theEvent.phase。

于 2012-12-12T08:04:44.210 回答
4

添加到@Sean Rich 答案。

contentView是和NSClipView之间的。NSScrollViewNSCollectionView

情节提要中的剪辑视图图片

为此,ClipView需要将两者都设置为postsBoundsChangedNotifications 并且应该在通知对象中传递。

self.clipView.postsBoundsChangedNotifications = true

NotificationCenter.default.addObserver(self,
                                       selector: #selector(collectionViewDidScroll(notification:)),
                                       name: NSView.boundsDidChangeNotification,
                                       object: self.clipView)
于 2018-05-06T03:45:20.377 回答
0

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

    }
于 2019-03-26T10:28:22.827 回答