Bla*_*end 14 objective-c uigesturerecognizer ios uicollectionview swift
我有一个UICollectionView有不同的项目.当我点击一个项目时,我使用:
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
Run Code Online (Sandbox Code Playgroud)
找出被触摸的内容然后基本上将该视图的alpha设置为0以隐藏它.一切正常.现在我想要做的是当你点击UICollectionViewCell所有视图周围的空白区域然后再次出现.我无法找到一种方法,可以让我知道细胞周围的白色空间何时被触摸.有没有一个好方法呢?我已经尝试设置手势识别器,但是当我这样做时,我的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
Run Code Online (Sandbox Code Playgroud)
不叫.有没有办法实现手势识别器,并从那里确定是否轻敲了一个单元格,如果是这样隐藏该单元格,否则显示所有隐藏的单元格?谢谢.
Ant*_*ine 20
我已经设法通过使用UITapGestureRecognizeron 来解决这个问题UICollectionView backgroundView.它在Swift中,但想法很明确:
self.tapGestureRecognizer = UITapGestureRecognizer(target: self, action: "handleTap:")
self.tapGestureRecognizer.delegate = self
self.collectionView.backgroundView = UIView(frame:self.collectionView.bounds)
self.collectionView.backgroundView!.addGestureRecognizer(tapGestureRecognizer)
Run Code Online (Sandbox Code Playgroud)
回调:
func handleTap(recognizer: UITapGestureRecognizer) {
// Handle the tap gesture
}
Run Code Online (Sandbox Code Playgroud)
我在项目中遇到了类似情况,并通过以下操作解决了该问题:
let tapGestureRecogniser = UITapGestureRecognizer(target: self, action: #selector(handleTapEmptySpaceGesture))
tapGestureRecogniser.delegate = self
collectionView.addGestureRecognizer(tapGestureRecogniser)
Run Code Online (Sandbox Code Playgroud)
实施UIGestureRecognizerDelegate协议
然后在协议中实现以下功能:
func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
// only handle tapping empty space (i.e. not a cell)
let point = gestureRecognizer.locationInView(collectionView)
let indexPath = collectionView.indexPathForItemAtPoint(point)
return indexPath == nil
}
Run Code Online (Sandbox Code Playgroud)
基本上,如果单击是在单元格上,则手势识别器不会开始,从而允许正常的选择/取消选择委托运行。否则,如果它在空白空间中,识别器将处理点击并运行其处理程序。
小智 0
仅当用户选择collectionViewCell时才会调用didSelectItem委托方法。单元格之间的间距可能会根据每个单元格的大小而变化,您只能指定最小间距。要通过更改 zindex 来接收触摸,将supplementaryView 保留为backgroundView,并为其添加触摸检测。希望对你有帮助。:)
| 归档时间: |
|
| 查看次数: |
5002 次 |
| 最近记录: |