I have a NSScrollview
nested inside another NSScrollview
. How do i make the inner view handle horizontal scrolling only? Vertical scrolling should move the outer view.
Currently i pass the scrollWheel:
event to the outer view from the inner view, but it is very slow.
This is my subclass of NSScrollView that does what you are asking. Since it is merely passing the events it doesn't care about up the responder chain it should be as performant as if it weren't a subclass (or at least close)
h file
and m
Swift 4.
Answer is a translation of Jakob's excellent answer above, with the addition of a direction flag.
As mentioned in the comments on his answer, there can be subtle complications if this isn't done right. When simply forwarding all scrollWheel() calls to the next responder, I was seeing issues with NSTrackingAreas not being updated correctly and tooltips and cursor styles over NSTextView views being misaligned.
I also had the problem of nested scroll views. The inner scroll view should scroll horizontally, and the outer should scroll vertically.
When handling scroll events from magic mouse / trackpad, it is important to pick only one of the scroll views for each gesture, otherwise you will see odd jerking when your fingers don't move perfectly straight. You should also ensure that tapping the trackpad with two fingers shows both scrollers.
When handling legacy scroll events from mighty mouse or mice with old fashioned scroll wheels, you must pick the right scroll view for each event, because there is no gesture phase information in the events.
This is my subclass for the inner scroll view, tested only in Mountain Lion:
My implementation does not always forward Gesture cancel events correctly, but at least in 10.8 this does not cause problems.