【问题标题】:How to make mouse wheel scroll horizontally with AppKit on macOS如何在 macOS 上使用 AppKit 使鼠标滚轮水平滚动
【发布时间】:2020-11-25 09:15:14
【问题描述】:

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

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

【问题讨论】:

    标签: swift macos cocoa appkit


    【解决方案1】:

    您可以继承 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)
            }
        }
    }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-25
      • 2016-03-14
      • 2011-01-21
      • 2013-10-21
      • 1970-01-01
      相关资源
      最近更新 更多