macos 滚动 NSScrollView 时的回调?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5169355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Callbacks When an NSScrollView is Scrolled?
提问by nonamelive
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:
我正在制作一个 Mac 应用程序,它需要知道用户何时滚动NSScrollView
,但是,我找不到任何类似 的方法UIScrollView
,它具有以下委托方法:
– scrollViewDidScroll:
– scrollViewWillBeginDragging:
– scrollViewDidEndDragging:willDecelerate:
– scrollViewShouldScrollToTop:
– scrollViewDidScrollToTop:
– scrollViewWillBeginDecelerating:
– scrollViewDidEndDecelerating:
Can I have the similar delegate methods for the App Kit? Thanks in advance.
我可以为 App Kit 提供类似的委托方法吗?提前致谢。
Kai.
凯。
回答by Sean Rich
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];
回答by Alexei Zaitsev
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.momentumPhaseinstead of theEvent.phase for event phases.
NSScrollView类的,但再检查theEvent.momentumPhase而不是theEvent.phase事件阶段。
回答by bauerMusic
Adding to @Sean Rich answer.
添加到@Sean Rich 的答案。
The contentView
is the NSClipView
between the NSScrollView
and NSCollectionView
.
该contentView
是NSClipView
之间NSScrollView
和NSCollectionView
。
For this to work, both the ClipView
needs to be set to postsBoundsChangedNotifications
andshould be passed in the notification object.
为此,ClipView
需要将两者设置为postsBoundsChangedNotifications
并且应该在通知对象中传递。
self.clipView.postsBoundsChangedNotifications = true
NotificationCenter.default.addObserver(self,
selector: #selector(collectionViewDidScroll(notification:)),
name: NSView.boundsDidChangeNotification,
object: self.clipView)
回答by wolffan
Update for Swift 4:
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
编辑:mac 中的集合不是从滚动视图继承的。正确更新
回答by ingconti
my two cents for swift 4.2 OSX:
我 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){
}