可可 NSScrollView 不会将 scrollWheel 事件传递给下一个响应者 [英] cocoa NSScrollView not pass scrollWheel event to next responder

查看:29
本文介绍了可可 NSScrollView 不会将 scrollWheel 事件传递给下一个响应者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,我不知道为什么 NSScrollView 没有将 scrollWheel 事件传递给下一个响应者.

My question is, I don’t know why NSScrollView isn’t pass scrollWheel event to next responder.

我必须手动添加代码

class MyScrollView: NSScrollView {
    override func scrollWheel(with event: NSEvent) {
        super.scrollWheel(with: event)

        // do something

        // If you don't use these codes, the next responder will not receive the event.
        if let responder = self.nextResponder {
            responder.scrollWheel(with: event)
        }
    }
}

现在,我知道如何解决这个问题,但我不知道为什么会出现这个问题.

Now, I know how to fix that but I don't know why there has this problem.

有没有人有关于这种情况的官方信息?

Does anyone have official information about this situation?

推荐答案

正如 Marek H 所指出的,如果一个对象处理事件,那么它通常会使用该事件并且不会将其向上传递到响应者链.在这种情况下,如果 scrollView 滚动,那么它的 superView 绝对不应该对滚动事件做任何事情.

As Marek H noted, if an object handles the event, then it typically consumes the event and does not pass it up the responder chain. In this case, if the scrollView scrolls, then it's superView definitely should not do anything with the scroll event.

其次,覆盖 scrollWheel(with) 将关闭响应式滚动.

Second, overriding scrollWheel(with) will turn off responsive scrolling.

答案:你真正想做的是覆盖func WantedForwardedScrollEvents(for axis: NSEvent.GestureAxis) ->Bool 在您的 (Custom View(NSView)) 类中.https://developer.apple.com/documentation/appkit/nsresponder/1534209-想要转发滚动事件

Answer: What you really want to do is override func wantsForwardedScrollEvents(for axis: NSEvent.GestureAxis) -> Bool in your (Custom View(NSView)) class. https://developer.apple.com/documentation/appkit/nsresponder/1534209-wantsforwardedscrollevents

NSScrollView 将尊重这一点,并在不使用它们实际滚动或橡皮筋时转发 scrollWheel 事件.此外,它还兼容响应式滚动.

NSScrollView will respect this and forward the scrollWheel events when it doesn't otherwise use them to actually scroll or rubber-band. Plus it's responsive scrolling compatible.

这篇关于可可 NSScrollView 不会将 scrollWheel 事件传递给下一个响应者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆