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

标签 swift macos cocoa 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)
        }
    }
}

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

关于swift - 如何在 macOS 上使用 AppKit 使鼠标滚轮水平滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65001621/

相关文章:

macos - 将整数附加到字符串 Bash 脚本 OSX

objective-c - CGRect 助手?

ios - 注释显示标注而不是开始拖放操作

swift - 按数字范围过滤数组

ios - 使用回调重现 UIAlertViewController

macos - NSTableView不会压缩

objective-c - 将 View 层次结构绘制到 Cocoa 中的特定上下文中

ios - 如何删除 UITableView 中的行并更新 UITableViewCell 中的标签

objective-c - 当委托(delegate)不再被引用时,在 `[NSApp run]` 处崩溃

objective-c - NSNotification 概念 - 哪段代码去哪里?