如何在保持单元格选择的同时向 UICollectionView 添加点击手势? [英] How to add tap gesture to UICollectionView , while maintaining cell selection?

查看:11
本文介绍了如何在保持单元格选择的同时向 UICollectionView 添加点击手势?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

UICollectionView 中添加单击手势,不要妨碍单元格选择.

Add a single tap gesture to UICollectionView, do not get in the way of cell selection.

我想要在 collectionView 的 no-cell 部分上进行其他点击.

I want some other taps on the no-cell part of the collectionView.

使用 XCode8、Swift 3.

Using XCode8, Swift 3.

override func viewDidLoad() {
    ...
    collectionView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(tap)))
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    print(indexPath)
}

func tap(sender: UITapGestureRecognizer){
    print("tapped")
}

结果

是的,它现在妨碍了.当您点击单元格时,它会记录已点击".

Result

Yeah, it gets in the way now. When you tap on cell, it logs "tapped".

  • 我检查了collectionView 和单元格的hitTest 返回值.两者都返回了被点击的单元格,这意味着它们形成了一个 Cell -> CollectionView
  • 的响应者链
  • 单元格上没有手势
  • collectionView 上的 3 个手势,似乎没有人使用单元格选择
    • UIScrollViewDelayedTouchesBeganGestureRecognizer
    • UIScrollViewPanGestureRecognizer
    • UITapGestureRecognizer

    找不到更多的踪迹.关于如何实现单元格选择或完成此任务的任何想法?

    Couldn't find more trace. Any ideas on how cell selection is implemented or to achieve this task?

    推荐答案

    当你想添加一个手势识别器,而不是从目标视图窃取触摸时,你应该设置 UIGestureRecognizer.cancelsTouchesInView 为您的 gestureRecognizer 实例设置为 false.

    Whenever you want to add a gesture recognizer, but not steal the touches from the target view, you should set UIGestureRecognizer.cancelsTouchesInView for your gestureRecognizer instance to false.

    这篇关于如何在保持单元格选择的同时向 UICollectionView 添加点击手势?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆