如何在 macOS 上使用 AppKit 使鼠标滚轮水平滚动

How to make mouse wheel scroll horizontally with AppKit on macOS

我的应用有一个只有一行的 NSCollectionView。我想用鼠标滚轮让它水平滚动,但不想同时按下shift键

那么如何在 macOS 上使用 AppKit 转换滚动方向

您可以继承 NSCollectionView 并覆盖 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)
        }
    }
}

你可以试试看,希望对你有帮助!