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

查看:418
本文介绍了如何在保持单元格选择的同时向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返回值。两者都返回了tapped单元格,这意味着它们形成了一个响应者链的Cell - > CollectionView

  • 没有关于单元格的手势

  • 3手势在collectionView上,没有人似乎使用单元格选择


    • UIScrollViewDelayedTouchesBeganGestureRecognizer

    • UIScrollViewPanGestureRecognizer

    • UITapGestureRecognizer

    • I check the hitTest return value of the collectionView and the cell. Both returned the tapped cell, which means they form a responder chain of Cell -> CollectionView
    • no gestures on the cell
    • 3 gestures on collectionView, no one seems to work with the cell select
      • UIScrollViewDelayedTouchesBeganGestureRecognizer
      • UIScrollViewPanGestureRecognizer
      • UITapGestureRecognizer

      找不到更多的痕迹。关于如何实现细胞选择或实现此任务的任何想法?

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

      推荐答案

      每当你想要添加一个手势识别器,但不是从目标视图中窃取触摸,您应该设置 UIGestureRecognizer。将 gestureRecognizer 实例的cancelsTouchesInView 取消为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天全站免登陆