2

I am implementing a single row carousel view by subclassing UICollectionView and UICollectionFlowLayout. The visual looks similar to the following: enter image description here

I want to only enable touch on the middle cell and disable the rest. Currently, I put the enabling/disabling logics in the delegate class as follow:

- (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;
}

I would like to move this logics into my UICollectionView subclass, as it feels neater. So, is there a way for me to intercept the delegate calls in my UICollectionView subclass and add the above logics?

crab oz
  • 635
  • 7
  • 16
  • Did you try `self.delegate = myViewController;` ? – Roman Simenok Apr 12 '16 at 12:33
  • It would be easier to intercept shouldSelect/didSelect cell and work out if that cell is in the centre IMO. – Sean Lintern Apr 12 '16 at 12:34
  • @RomanSimenok the current disabling/enabling logics is already implemented in the a viewcontroller which is a delegate of my UICollectionView subclass – crab oz Apr 12 '16 at 12:37
  • @SeanLintern88 There are some touchable UI elements in each cell, such as UIButton. This is the reason why I want to disable non-centre cells. I am not sure shouldSelect/didSelect will work. – crab oz Apr 12 '16 at 12:39
  • Is the code above in your view controller? Does your view controller implement other collection view or scroll view delegate methods? Is it just the enable/disable logic that you want to move to your UICollectionView subclass? – Mike Taverne Apr 12 '16 at 14:32
  • I just want to hide the disabling/enabling logics in the UICollectionView subclass. – crab oz Apr 12 '16 at 23:27

0 Answers0