1
votes

I have a UICollectionView using custom UICollectionViewCell which in turn contains UIScrollView containing UIImageView.

The UIScrollView has several gesture recognizers attached to it (tap, double tap). I use the UIScrollView in order to zoom into the images.

Problem is the only way I was able to receive the touch events in the UICollectionView was to disable user interaction in the UIScrollView. Despite this the UICollectionView was scrolling, so apparently some of the gestures on the UIScrollView were being passed along to the parent UICollectionView)

I tried another solution by adding these methods in the UIScrollView (custom class)

@interface MyScrollView : UIScrollView

@end

@implentation MyScrollView
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [[self nextResponder] touchesEnded:touches withEvent:event];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [[self nextResponder] touchesBegan:touches withEvent:event];
}

-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    [[self nextResponder] touchesCancelled:touches withEvent:event];
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    [[self nextResponder] touchesMoved:touches withEvent:event];
}
@end

This helped in allowing the collectionview to receive touches, BUT when swiping up in the scrollview, the collectionview would stop receiving future touches...

What is the proper way to pass the touch event (single/double tap) ?

2

2 Answers

0
votes

I have this exact setup and this is what I did

  1. I added a touch gesture to my image view

    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bannerTapped:)];
    singleTap.numberOfTapsRequired = 1;
    singleTap.numberOfTouchesRequired = 1;
    cell.conversationImageView.tag = indexPath.row;
    [cell.conversationImageView addGestureRecognizer:singleTap];
    [cell.conversationImageView setUserInteractionEnabled:YES];
    
  2. Then I added this

    - (void)bannerTapped:(UIGestureRecognizer *)gestureRecognizer {
        NSLog(@"%@", [gestureRecognizer view]);
        NSLog(@"the tag is %d", [gestureRecognizer view].tag);
    
        //do something here based on the tag which tells me what row I'm in
    }
    

Hope maybe this can work for you