如何在所有单元格周围的空白区域中接收对 UICollectionView 的触摸 [英] How to receive touches on a UICollectionView in the blank space around all cells

查看:87
本文介绍了如何在所有单元格周围的空白区域中接收对 UICollectionView 的触摸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UICollectionView 里面有不同的项目.当我点击一个项目时,我使用:

I have a UICollectionView that has different items in it. When I tap on an item, I use:

 -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath

找出被触摸的内容,然后基本上将该视图的 alpha 设置为 0 以隐藏它.这一切正常.现在我想做的是当您点击所有 UICollectionViewCell 周围的空白区域时,所有视图都会再次出现.我无法找到一种方法来让我知道何时触摸了单元格周围的空白区域.有没有好的方法可以做到这一点?我试过设置手势识别器,但是当我这样做时,我的方法

to figure out what was touched and then basically set the alpha of that view to 0 to hide it. That all works fine. Now what I would like to do is when you tap on the white space surrounding all of the UICollectionViewCells all of the views then appear again. I am having trouble finding a method that will allow me to know when the white space around the cells has been touched. Is there a good way to do that? I have tried setting up a gesture recognizer, but when I do that, my method

 -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath

没有被调用.有什么方法可以实现手势识别器,并从那里确定是否点击了一个单元格,如果是,则隐藏该单元格,否则显示所有隐藏的单元格?谢谢.

isn't called. Is there some way to to just implement the gesture recognizer and from there determine if a cell was tapped and if so hide that cell, else show all the hidden cells? Thanks.

推荐答案

我通过在 UICollectionView backgroundView 上使用 UITapGestureRecognizer 设法解决了这个问题.它在 Swift 中,但想法很明确:

I've managed to fix this problem by using a UITapGestureRecognizer on the UICollectionView backgroundView. It's in Swift, but the idea is clear:

self.tapGestureRecognizer = UITapGestureRecognizer(target: self, action: "handleTap:")
self.tapGestureRecognizer.delegate = self

self.collectionView.backgroundView = UIView(frame:self.collectionView.bounds)
self.collectionView.backgroundView!.addGestureRecognizer(tapGestureRecognizer)

和回调:

func handleTap(recognizer: UITapGestureRecognizer) {
    // Handle the tap gesture
}

这篇关于如何在所有单元格周围的空白区域中接收对 UICollectionView 的触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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