Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I disable the vertical scrolling of a NSScrollView ?

How can I disable the vertical scrolling of a NSScrollView ?

I can't find a quick solution on Google.

Thanks

like image 420
aneuryzm Avatar asked Mar 21 '12 08:03

aneuryzm


2 Answers

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]);
like image 98
Ivan Androsenko Avatar answered Nov 15 '22 04:11

Ivan Androsenko


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.

like image 43
Brad Hesse Avatar answered Nov 15 '22 06:11

Brad Hesse