我通过子类化 UICollectionView 和 UICollectionFlowLayout 来实现单行轮播视图。视觉效果类似于以下内容:
我只想在中间单元格上启用触摸并禁用其余部分。目前,我将启用/禁用逻辑放在委托类中,如下所示:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kButtonCollectionViewCellIdentifier
forIndexPath:indexPath];
// ...
cell.userInteractionEnabled = NO;
return cell;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;
{
CGPoint midPoint= CGPointMake(self.collectionView.center.x + self.collectionView.contentOffset.x,
self.collectionView.center.y + self.collectionView.contentOffset.y);
NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:midPoint];
UICollectionViewCell *cell = [self.collectionView cellForItemAtIndexPath:indexPath];
cell.userInteractionEnabled = YES;
}
我想将此逻辑移到我的 UICollectionView 子类中,因为它感觉更整洁。那么,有没有办法让我在我的 UICollectionView 子类中拦截委托调用并添加上述逻辑?