2015-11-04 123 views

回答

5

後谷歌的時候,我已經找到了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 
0

那是可以設置的CollectionView的backgroundView用戶交互不啓用,但要確保collectionViewCell做,使之能將tapEvent傳遞給下一個響應者。

- (void)viewDidLoad { 
    theCollectionView.userInteractionEnabled = NO; 
} 

我希望這會有所幫助。

+0

這樣UICollectionViewCell無法響應用戶交互。 – tounaobun

+0

糟糕...好吧 – iGuan7u

8

@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全部設置。

+0

提供Swift版本的好工作。 – tounaobun

+0

我有類似的情況:UICollectionViewCell在另一個UICollectionViewCell中。但是,我只能從包含其他單元的單元中檢測水龍頭(通過didSelectItem委託方法)。但我需要檢測內部單元的水龍頭。這不起作用。 – vikzilla

+0

@vikzilla你有沒有找到解決方案?我也有同樣的問題。 –

相關問題