简体   繁体   English

可可 NSScrollView 不会将 scrollWheel 事件传递给下一个响应者

[英]cocoa NSScrollView not pass scrollWheel event to next responder

My question is, I don't know why NSScrollView isn't pass scrollWheel event to next responder.我的问题是,我不知道为什么 NSScrollView 没有将 scrollWheel 事件传递给下一个响应者。

I have to manually add the code我必须手动添加代码

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?有没有人有关于这种情况的官方信息?

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.正如 Marek H 所指出的那样,如果一个对象处理了事件,那么它通常会消费该事件并且不会将其向上传递到响应者链。 In this case, if the scrollView scrolls, then it's superView definitely should not do anything with the scroll event.在这种情况下,如果 scrollView 滚动,那么它的 superView 绝对不应该对滚动事件做任何事情。

Second, overriding scrollWheel(with) will turn off responsive scrolling.其次,覆盖 scrollWheel(with) 将关闭响应式滚动。

Answer: What you really want to do is override func wantsForwardedScrollEvents(for axis: NSEvent.GestureAxis) -> Bool in your (Custom View(NSView)) class.答案:您真正想要做的是在您的 (Custom View(NSView)) 类中覆盖func wantsForwardedScrollEvents(for axis: NSEvent.GestureAxis) -> Bool https://developer.apple.com/documentation/appkit/nsresponder/1534209-wantsforwardedscrollevents https://developer.apple.com/documentation/appkit/nsresponder/1534209-wantsforwardedscrollevents

NSScrollView will respect this and forward the scrollWheel events when it doesn't otherwise use them to actually scroll or rubber-band. NSScrollView 将尊重这一点,并在不使用它们实际滚动或橡皮筋时转发 scrollWheel 事件。 Plus it's responsive scrolling compatible.此外,它与响应式滚动兼容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM