惯性滚动和-ScrollWheel:在 OS X 上

发布于 2024-11-10 14:51:41 字数 888 浏览 3 评论 0原文

问题:

我有一个 NSScrollView。我用它来实现一个自定义的“表视图”,其中的数据行实际上是 NSView。 (注意:这不是 NSTableView 的实例。)

当我垂直滚动(没有水平滚动)时,我使用boundsChanged 通知来添加(作为滚动视图的 contentView 的子视图)变得可见的 NSView(带有框架的 NSView)与scrollView的文档可见矩形相交)并删除不再可见的那些(scrollView可见矩形之外的框架)。

除了惯性滚动之外,该过程工作得非常好。如果我将光标放在单元格 X 上,然后轻弹触控板以惯性快速向下滚动,则单元格 X 会快速离开可见矩形,并因此从滚动视图的 contentView 中删除。但是,这会破坏惯性滚动。如果我不将单元格 X 作为子视图删除,那么惯性滚动就可以完美工作。

我需要什么:

一种保持惯性滚动的方法,同时仍然删除用户开始滚动手势时光标恰好位于其顶部的 NSView。

我尝试过的:

我研究了 NSResponder 的方法:

-scrollWheel:(NSEvent *)theEvent 

默认实现将scrollWheel传递给下一个响应者。因此,我对 NSScrollView 进行了子类化并实现了此方法,以尝试阻止它将scrollWheel 事件传递给scrollView 的contentView 内的各个子View。没用。

然后我进入我的 NSViews(我添加到 contentView 的那些)并覆盖scrollWheel以将事件传递回scrollView本身。没用。

在这两种情况下我仍然会滚动,但不会有惯性。

有什么想法吗?谢谢!

THE PROBLEM:

I have an NSScrollView. I'm using it to implement a custom "Table View" with rows of data that are actually NSViews. (Note: this is not an instance of NSTableView.)

As I scroll vertically (there is no horizontal scrolling), I use a boundsChanged notification to add (as subviews of the scrollView's contentView) the NSViews that become visible (the ones with frames that intersect the scrollView's document visible rect) and to remove the ones that are no longer visible (frames outside of the scrollView's visible rect.)

The process works wonderfully except when it comes to inertial scrolling. If I have my cursor over cell X and I flick the trackpad to scroll downwards FAST with inertia, cell X quickly leaves the visible rect and, as such, is removed from the scrollView's contentView. BUT, that kills inertial scrolling. If I do NOT remove cell X as a subView, then inertial scrolling works perfectly.

WHAT I NEED:

A way to keep inertial scrolling while still removing the NSView that the cursor happened to be on top of when the user started the scrolling gesture.

WHAT I'VE TRIED:

I've looked at NSResponder's method:

-scrollWheel:(NSEvent *)theEvent 

The default implementation passes scrollWheel to the next responder. So, I subclassed NSScrollView and implemented this method to try to stop it from passing the scrollWheel event to the individual subViews inside the scrollView's contentView. Didn't work.

So then I went into my NSViews (the ones I'm adding to the contentView) and overrode scrollWheel to pass the event back to the scrollView itself. Didn't work.

I still get scrolling in both cases, but not with inertia.

Any ideas? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离鸿 2024-11-17 14:51:41

我还没有在 Cocoa 中做过这件事,但我可能会考虑简单地回收你的 NSView 对象,一旦它离开可见矩形,通过删除它的子视图,然后更改它的框架就位以从顶部滚动回可见矩形。

显然,您可以通过简单地更新其框架来做到这一点,并且避免将其删除并重新添加到 NSScrollView 中。

I haven't done this exact thing in Cocoa, but I'd probably be thinking about simply recycling your NSView object, as soon as its off the visible rect, by removing its subviews, then changing its frame to be in place to scroll back onto the visible rect from the top.

You can obviously do this by simply updating its frame, and avoid having to remove and re-add it to the NSScrollView.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文