Michael
Michael

Reputation: 33

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.

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?

Upvotes: 0

Views: 592

Answers (1)

RJL
RJL

Reputation: 36

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.

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

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 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.

Upvotes: 2

Related Questions