微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我已经尝试了子类化UICollectionView并覆盖touchesBegan:withEvent:和hitTest:WithEvent:,当我触摸单元格时,这两个方法都会触发.但是,如果我触摸单元格之间的空间,则根本不会发生任何事情.这是我创造的:
@interface WSImagegalleryCollectionView : UICollectionView
@end

..和..

@implementation WSImagegalleryCollectionView

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"touches began");
    [super touchesBegan:touches withEvent:event];
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    NSLog(@"Hit test reached");
    return [super hitTest:point withEvent:event];
}

@end

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

解决方法

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

原文地址:https://www.jb51.cc/iOS/327924.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐