ScrollView不能将scrollWheel事件传递给下一个

ScrollView不能将scrollWheel事件传递给下一个

本文介绍了可可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.

有人知道这种情况吗?

推荐答案

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

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 wantForwardedScrollEvents(对于轴:NSEvent.GestureAxis)->(Custom View(NSView))类中的Bool . https://developer.apple.com/documentation/appkit/nsresponder/1534209-wantforwardedscrollevents

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事件传递给下一个响应者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 05:51