在修改原來同事留下的代碼,collectionView?所有代理都設置了购公,確保沒有任何問題之后但就是不調用collectionView didSelectItemAtIndexPath代理方法;
最終發(fā)現他在collectionView上添加了UITapGestureRecognizer手勢祈噪;
解決方案:UITapGestureRecognizer手勢有個屬性?tap.cancelsTouchesInView雕擂,默認是肯定的踊东,設置為false即可贰健;
蘋果官方文檔如下解釋:
// default is YES. causes touchesCancelled:withEvent: or pressesCancelled:withEvent: to be sent to the view for all touches or presses recognized as part of this gesture immediately before the action method is called.
英文不好胞四,不過大致意思應該是觸摸事件被取消,也就是說觸摸事件沒有繼續(xù)往下傳遞伶椿,自然didSelectItemAtIndexPath代理方法也就不會觸發(fā)辜伟;
我的代碼:
? ? UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
? ? tap.numberOfTapsRequired = 1; // 單擊
? ? tap.cancelsTouchesInView = false;
? ? [_mainCollectionView addGestureRecognizer:tap];