gpt4 book ai didi

swift - 如何检测对 UICollectionView 中单元格 "only for a single finger"的双击?

转载 作者:行者123 更新时间:2023-11-28 07:26:06 25 4
gpt4 key购买 nike

我有一个包含大量单元格的 UICollectionView

我实现了两个功能:

  1. 单击一个单元格:推送一个 View 。
  2. 双击单元格:按 B View 。

问题是:

如果我同时点击两个单元格(或更多),它只会推送 1 一个 View 。在这种情况下没问题。

但是,如果我同时双击两个单元格(或更多),它会一个接一个地推送2(或更多) B View 。这会导致意外行为,应用有时会崩溃。

当有超过 2 个单元格被点击时,如何禁止其他单元格接收双击,并且只允许推送“1 B View ”

我添加了 doubleTapGesture.numberOfTouchesRequired = 1,但它不起作用。

这是我的代码:

@objc private func didReceiveDoubleTap(_ sender: ASCellNode) {
self.doubleTapDelegate?.myCellWasDoubleTapped?(self)
}

override func didLoad() {
super.didLoad()

let doubleTapGesture = UITapGestureRecognizer(target: self, action: #selector(MyCollectionCell.didReceiveDoubleTap(_:)))
doubleTapGesture.numberOfTapsRequired = 2
doubleTapGesture.delaysTouchesBegan = true
self.view.addGestureRecognizer(doubleTapGesture)
}

最佳答案

看看是否可以通过 bool 检查来防止多次调用委托(delegate)方法。

@objc private func didReceiveDoubleTap(_ sender: ASCellNode) {
if !processing {
processing = true // set processing = false where it's safe to do so.
self.doubleTapDelegate?.myCellWasDoubleTapped?(self)
}
}

关于swift - 如何检测对 UICollectionView 中单元格 "only for a single finger"的双击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56575947/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com