滚动NSScrollView时的回调?

时间:2011-03-02 15:02:57

标签: objective-c cocoa macos uiscrollview nsscrollview

我正在创建一个Mac应用程序,需要知道用户何时滚动NSScrollView,但是,我找不到像UIScrollView这样的方法,它有以下委托方法:< / p>

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

我可以为App Kit使用类似的委托方法吗?提前谢谢。

5 个答案:

答案 0 :(得分:67)

您可以通过监视滚动视图内容视图的范围来监视滚动视图的更改。首先设置内容视图以使用

发布其更改
[contentView setPostsBoundsChangedNotifications:YES];

然后使用

注册为这些通知的观察者
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(boundsDidChange:) name:NSViewBoundsDidChangeNotification object:contentView]; 

答案 1 :(得分:3)

最近有同样的问题......要稍微模仿减速回调,可以覆盖

-(void) scrollWheel:(NSEvent *)theEvent 
NSScrollView类的

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

答案 2 :(得分:3)

更新swift 4:

scrollView.contentView.postsBoundsChangedNotifications

此外,电话是: NotificationCenter.default.addObserver(self,selector:#selector(boundsChange),name:NSView.boundsDidChangeNotification,object:self)

编辑:mac中的集合不会从scrollview继承。正确更新

答案 3 :(得分:2)

添加到@Sean Rich answer。

contentViewNSClipViewNSScrollView之间的NSCollectionView

Picture of clip view from the Storyboard

要实现这一点,ClipView需要设置为postsBoundsChangedNotifications 都应该在通知对象中传递。

self.clipView.postsBoundsChangedNotifications = true

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

答案 4 :(得分:0)

我花了2美分换取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){

    }
相关问题