NSCollectionView 不响应鼠标/触摸事件

时间:2021-02-12 13:28:44

标签: swift macos cocoa

我对 MacOS 开发很陌生,但我是一名经验丰富的 iOS 开发人员。

我有一个 NSCollectionView 我已经连接到一个 NSCollectionViewFlowLayout 用于水平滚动,我添加了很多单元格,它几乎可以正常工作与魔术鼠标滑动滚动一起使用,它会忽略所有鼠标点击事件,因此我无法使用鼠标滚动它。我可以向单元格添加一个按钮,它会检测点击操作。

在 iOS 上,userInteractionEnabled 已被设置为 false 或其他什么。

有什么想法吗?

提前致谢

enter image description here

class ViewController: NSViewController {
  @IBOutlet weak var slidesCollection: NSCollectionView!
  var slide : [String] = ["one", "two","one", "two","one", "two","one", "two","one", "two","one", "two","one", "two","one", "two","one", "two","one", "two","one", "two","one", "two","one", "two","one", "two","one", "two","one", "two","one", "two","one", "two","one", "two","one", "two"]
  override func viewDidLoad() {
    super.viewDidLoad()
    let flowLayout = NSCollectionViewFlowLayout()
    flowLayout.itemSize = CGSize(width: 100, height: 100)
    flowLayout.scrollDirection = .horizontal
    flowLayout.minimumInteritemSpacing = 10.0
    flowLayout.sectionInset = NSEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    slidesCollection.collectionViewLayout = flowLayout
    slidesCollection.dataSource = self
    
    let nib = NSNib(nibNamed: Cell.name, bundle: nil)!
    slidesCollection.register(nib, forItemWithIdentifier: Cell.identifier())
  }
}

extension ViewController: NSCollectionViewDataSource {
  func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
    return slide.count
  }
  
  func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
    let item = collectionView.makeItem(withIdentifier: Cell.identifier(), for: indexPath)
    
    return item
  }
}

1 个答案:

答案 0 :(得分:0)

好吧,这似乎很正常,我只是注意到其他 MacOS 应用程序在使用 Magic mouse 时也做同样的事情!想想会回到 iOS 开发,上次我做桌面应用程序开发是近 20 年前的 Windows。 !