這是視圖結構:將UICollectionView觸發事件傳遞給其父母UITableViewCell
外部視圖是UITableView
。
在UITableViewCell
裏面,有一個UICollectionView
。並注意集合視圖單元格之間有一些黑色的間距。
當我點擊UICollectionView中的間距時,我想讓touch事件傳遞給UITableViewCell
。
這是視圖結構:將UICollectionView觸發事件傳遞給其父母UITableViewCell
外部視圖是UITableView
。
在UITableViewCell
裏面,有一個UICollectionView
。並注意集合視圖單元格之間有一些黑色的間距。
當我點擊UICollectionView中的間距時,我想讓touch事件傳遞給UITableViewCell
。
後谷歌的時候,我已經找到了solution.Just繼承UICollectionView
類並重寫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用戶交互不啓用,但要確保collectionViewCell做,使之能將tapEvent傳遞給下一個響應者。
- (void)viewDidLoad {
theCollectionView.userInteractionEnabled = NO;
}
我希望這會有所幫助。
@tounaobun優秀的回答。經測試,它按預期工作:
1)如果您點擊集合視圖中不是一個項目的任何地方,那麼下面的表格單元格將選擇得很好。
2)如果在集合視圖中的項目輕按,然後將表格單元格將不會選擇,您可以用集合視圖正常互動(又名滾動,調用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,你應該b全部設置。
這樣UICollectionViewCell無法響應用戶交互。 – tounaobun
糟糕...好吧 – iGuan7u