UICollectionView 作为
H:[collectionView(==270)]|
添加到 UIVew和 V:|-(70)-[collectionView]-(70)-|
.我有两个要求:
1) 只有 UICollectionViewCell 的蓝色部分应该触发
collectionView::didSelectItemAtIndexPath:
方法。我已成功实现- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
UIView *hitView = [super hitTest:point withEvent:event];
if (hitView == self.customView) {
return self;
}
return nil;
}
在自定义类 UIColectionViewCell 上。
customView
属性是单元格的蓝色/红色 UIView。这按预期工作。
2)我希望将在 UICollectionViewCell 的绿色部分或 UICollectionVIew 本身(此处为白色,不透明度为 0.5 的背景)中完成的任何手势(Tap/LongTap/Pan/...)传递给 super View .例如下面的绿松石 UIButtion。
绿色部分也不应该滚动 CollectionView .. 它必须对任何手势完全透明。
我怎样才能做到这一点?尝试了很多不同的方法,但都没有奏效。我知道如何使用常规 UIView 来完成,但无法让它在 UICollectionView 及其单元格上工作。
请记住,还有其他 UIView 或 UIButtion 可以交互并放置在 Collection View 的绿色部分下方。绿色稍后将只是 UIClearColor。
建议让 UICollectionView 更小的宽度(蓝色部分的宽度)不是一个选项,因为在某些情况下 UICell 的蓝色/红色部分必须拉伸(stretch)到单元格的整个宽度。
最佳答案
这是上述示例的最终解决方案:
1) 只需要可点击的红色/蓝色部分。
这基本保持不变。我引用了蓝色/红色 UIView 名称 customView
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
UIView *hitView = [super hitTest:point withEvent:event];
if (hitView == self.customView) {
return self;
}
return nil;
}
2) UICollectionView 内未在蓝色/红色 UIView 上完成的任何交互都应被忽略。
继承 UICollectionView 并添加覆盖此方法:
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
NSIndexPath *indexPath = [self indexPathForItemAtPoint:point];
LCollectionViewCell *cell = (LCollectionViewCell*)[self cellForItemAtIndexPath:indexPath];
if (cell && [self convertPoint:point toView:cell.customView].x >= 0) {
return YES;
}
return NO;
}
这将检查 CGPoint 是否在
customView
上完成。 .有了这个,我们还支持可变宽度:关于ios - 将 UICollectionView 的某些部分的触摸传递给底层 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36463424/