How can I disable the vertical scrolling of a NSScrollView ?
I can't find a quick solution on Google.
Thanks
Try this in NSScrollView subclass:
- (void)scrollWheel:(NSEvent *)theEvent
{
[super scrollWheel:theEvent];
if ([theEvent deltaY] != 0.0)
{
[[self nextResponder] scrollWheel:theEvent];
}
}
Also you can log it by:
NSLog(@"user scrolled %f horizontally and %f vertically", [theEvent deltaX], [theEvent deltaY]);
I like Ivan's answer above. However, in my particular case, I had a table view (vertical scrolling) where each row contained a collection view (horizontal scrolling).
When users tried to scroll up/down, if there mouse was hovered over one of the collection views, they wouldn't be able to scroll up or down.
To resolve this, I subclassed the NSScrollView that contained the NSCollectionViews and I overrided this method with the following code:
override func scrollWheel(with event: NSEvent)
{
if (fabs(Double(event.deltaY)) > fabs(Double(event.deltaX))) {
self.nextResponder?.scrollWheel(with: event);
} else {
super.scrollWheel(with: event);
}
}
This way, it checks if the user is predominantly scrolling in one direction and handles it appropriately.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With