【问题标题】:NSCollectionView not responding to Mouse/touch eventsNSCollectionView 没有响应鼠标/触摸事件
【发布时间】:2021-02-12 13:28:44
【问题描述】:

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

我有一个 NSCollectionView 我已经连接到一个 NSCollectionViewFlowLayout 用于水平滚动,我已经添加了很多单元格,它几乎可以工作,它只是与魔术鼠标滑动滚动一起使用,它忽略所有鼠标点击事件,所以我不能用鼠标滚动它。我可以在单元格中添加一个按钮,并且确实可以检测到点击操作。

在 iOS 上,userInteractionEnabled 被设置为 false 之类的,我很难过。

有什么想法吗?

提前致谢

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
  }
}

【问题讨论】:

  • 我尝试了您的代码,但没有发现任何问题。您是否尝试过将布局切换为垂直?你是如何测试鼠标点击事件的?项目视图是否捕捉到滚动事件?
  • 我无法通过鼠标指针拖动集合视图以将所有单元格显示在右侧,只能通过神奇的鼠标滑动来进行测试。我还注意到 item selected 事件没有被触发,但这可能是别的东西。单元格非常简单,它只有那个我什至没有连接的标签

标签: swift macos cocoa


【解决方案1】:

好吧,这似乎很正常,我只是注意到其他 MacOS 应用程序对 Magic mouse 执行相同操作!想想将回到 iOS 开发,我上次做桌面应用程序开发是近 20 年前的 Windows。 !

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-24
    相关资源
    最近更新 更多