【发布时间】:2020-11-25 09:15:14
【问题描述】:
我的应用有一个只有一行的 NSCollectionView。我想用鼠标滚轮让它水平滚动,但不想同时按下 shift 键。
那么如何在 macOS 上使用 AppKit 转换滚动方向
【问题讨论】:
我的应用有一个只有一行的 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)
}
}
}
你可以试试,希望对你有帮助!
【讨论】: