r1cdjuabna1djauk1u
r1cdjuabna1djauk1u

Reputation: 445

How to make mouse wheel scroll horizontally with AppKit on macOS

My app has an NSCollectionView with only one row. I want to make it scroll horizontally with the mouse wheel, but don't want to press the shift button at the same time.

So how can I convert the scrolling direction with AppKit on macOS

Upvotes: 2

Views: 997

Answers (1)

Dee Wu
Dee Wu

Reputation: 66

You can subclass NSCollectionView and override scrollWheel.

import Cocoa

class YourCollectionView: NSCollectionView {

    override func scrollWheel(with event: NSEvent) {
        // get mouse wheel scroll offset
        let scrollOffset = event.scrollingDeltaY

        // scroll collectionView your self, maybe like this
        if let clipView = self.enclosingScrollView?.contentView as? NSClipView {
            let currentPoint = self.visibleRect.origin
            let newPoint = CGPoint(x: currentPoint.x + scrollOffset ,y: currentPoint.y)
            clipView.scroll(to: newPoint)
        }
    }
}

you can have a try, hope this can help you!

Upvotes: 4

Related Questions