ios – 如何在UICollectionView上触摸背景(即不是单元格)?

前端之家收集整理的这篇文章主要介绍了ios – 如何在UICollectionView上触摸背景(即不是单元格)?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经尝试了子类化UICollectionView并覆盖touchesBegan:withEvent:和hitTest:WithEvent:,当我触摸单元格时,这两个方法都会触发.但是,如果我触摸单元格之间的空间,则根本不会发生任何事情.这是我创造的:
  1. @interface WSImageGalleryCollectionView : UICollectionView
  2. @end

..和..

  1. @implementation WSImageGalleryCollectionView
  2.  
  3. - (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  4. {
  5. NSLog(@"Touches began");
  6. [super touchesBegan:touches withEvent:event];
  7. }
  8.  
  9. - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
  10. NSLog(@"Hit test reached");
  11. return [super hitTest:point withEvent:event];
  12. }
  13.  
  14. @end

注意:手势识别器似乎有完全相同的问题,这就是为什么我尝试使用touchesBegan进行更低级别的操作.

解决方法

您只需将视图设置为背景视图,然后就可以添加手势识别器:
  1. collectionView.backgroundView = [[UIView alloc] init];
  2. [collectionView.backgroundView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOnBackgroundRecognized)]];

猜你在找的iOS相关文章