将UICollectionView touch事件传递给它的父UITableViewCell
这是视图结构:
外部视图是一个UITableView
。
在UITableViewCell
里面,有一个UICollectionView
。 并注意集合视图单元格之间有一些黑色的间距。
当我点击UICollectionView中的空格时,我想让touch事件传递给UITableViewCell
。
优秀的回答@ tounaobun。 经过testing,并按预期工作:
1)如果你点击集合视图中不是一个项目的任何地方,那么下面的表格单元格将select得很好。
2)如果您点击集合视图中的项目,那么表格单元格将不会select,并且您可以正常地与集合视图进行交互(即,滚动,调用didSelectItem等)
我转换为快速参考:
Swift 3:
class TableCellCollectionView: UICollectionView { override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? { if let hitView = super.hitTest(point, with: event) { if hitView is TableCellCollectionView { return nil } else { return hitView } } else { return nil } }
只需将这个类定义添加到您的代码(我有一个实用工具文件),然后将集合视图的类从UICollectionView更改为TableCellCollectionView,你应该都设置。
谷歌后,我已经find了一个解决scheme。 UICollectionView
inheritanceUICollectionView
类并重写hitTest:withEvent
方法。
CustomCollectionView.h
@interface CustomCollectionView : UICollectionView @end
CustomCollectionView.m
@implementation CustomCollectionView - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { UIView *hitView = [super hitTest:point withEvent:event]; if ([hitView isKindOfClass:[self class]]) { // If it is class UICollectionView,just return nil. return nil; } // else return super implementation. return [super hitTest:point withEvent:event]; } @end
有可能设置collectionView的backgroundView UserInteraction不启用,但确保collectionViewCell做到了,以便它可以将tapEvent传递给下一个响应者。
- (void)viewDidLoad { theCollectionView.userInteractionEnabled = NO; }
我希望这会有所帮助。