collectionView 中的 TapGesture

TapGesture inside collectionView

我想将双击手势或其他一些功能添加到集合视图,以下载附加到所选单元格的文件。

我有一个双击手势识别器在工作,当你在集合视图中双击时,它会打印出该点。然而,只要我尝试双击一个单元格,内置手势识别器就会生效并且 didSelectItemAt 是 运行.

有没有办法在不触发 didSelectItemAt 中的代码的情况下添加双击?或在不向 viewController 添加按钮的情况下实现此功能的另一种方法的任何想法?

这是我在具有集合视图的 viewcontroller 的 viewDidLoad 中使用的手势代码:

let doubleTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(didDoubleTap))
    doubleTap.numberOfTapsRequired = 2
    self.collectionView.addGestureRecognizer(doubleTap)

还有我的 doubleTap 函数:

func didDoubleTap(gesture: UITapGestureRecognizer) {

    let point: CGPoint = gesture.location(in: self.collectionView)

    print(point)

    if let selectedIndexPath: IndexPath = self.collectionView.indexPathForItem(at: point) {
        let selectedCell: UICollectionViewCell = self.collectionView.cellForItem(at: selectedIndexPath as IndexPath)!
        print("cell \(selectedCell) was double tapped")
    }
}

请注意,我曾尝试将其添加到单元格本身,但我无法使其正常工作。该单元格是一个自定义的 UICollectionViewCell 并且没有 viewDidLoad 函数,因此要创建手势我必须创建一个委托来调用代码以从保存集合视图的 viewController 设置手势而我只是出现错误。

您应该将点击手势识别器添加到 cellForRowAtIndexpath 数据源方法中的每个单元格。

您可以在手势识别器上尝试 delaysTouchesBegan = true。这应该会延迟导致 didSelect 和 运行 手势识别器

你的动作的触摸处理