1
votes

I have a UICollection view which implements the "didSelectItemAtIndexPath..." method. When the cell is selected (tapped), a transition is made to another view controller. However, each cell also contains a vertical scroll view. I'd like the user to have the option of interacting with each cell to either swipe vertically to see the contents of the scroll view, or tap once to select the cell.

My first thought was to add two gesture recognizers to the cell, one for swiping and one for tapping, but I am not sure if it is a good idea to start adding more gesture recognizers to a UICollectionView and scrollview. I'm also not sure how I would tie the swipe gesture to the scrolling action of the scroll view.

I found this resource: http://uncorkedstudios.com/2013/05/03/using-uigesturerecognizers-with-uicollectionviews/, which suggests using requireGestureRecognizerToFail.

After hacking around with both of the above approaches, I do not feel any closer to figuring out how to do this and more importantly, how to do it properly. Can anyone suggest an advisable strategy for getting each UICollectionViewCell to respond to two unique gestures with separate actions?

EDIT I have tried the following approach to solve this problem, and still cannot get the two gesture recognizers working together:

1)To my collection view cell (a subclass of UICollectionViewCell) I added a UITapGestureRecognizer property that I set up in the init method:

self.tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(cellWasTapped:)]; 
self.tap.delegate = self; //this class is a UIGestureRecognizerDelegate 

2)The method cellWasTapped

- (void)cellWasTapped:(UITapGestureRecognizer *)sender
{
    NSLog(@"this cell was tapped!"); //temporary
}

3)Since the scroll view is a webView scroll view, I have added the gesture recognizer code at the end of the webView delegate method:

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [self.overlayView removeFromSuperview];
    CGSize contentSize = self.webView.scrollView.contentSize;
    CGSize viewSize = self.webView.bounds.size;
    float rw = viewSize.width / contentSize.width;
    self.webView.scrollView.minimumZoomScale = rw;
    self.webView.scrollView.maximumZoomScale = rw;
    self.webView.scrollView.zoomScale = rw;
    self.webView.scrollView.scrollEnabled = YES;
    self.webView.scrollView.contentOffset = CGPointMake(0, 0);
    self.webView.scrollView.size = CGSizeMake(self.webView.scrollView.contentSize.width,    
    self.webView.scrollView.contentSize.height - 5);
    self.webView.scrollView.delegate = self;

    [self addGestureRecognizer:self.tap];
    [self.tap requireGestureRecognizerToFail:self.webView.scrollView.panGestureRecognizer];
}

The problem is, the tap gesture recognizer only works BEFORE the webView contents appear in the web view. After the contents appear, then ONLY the scrolling of the web view works.

1
Do you have some code to show? I would add a second gesture and use requireGestureRecognizerToFail.Wain
I don't have any code - I can't see how to begin to approach this. I know how to add gesture recognizers to a view in general, but here I am lost. Let's say I add a tap gesture to the cell. Then I use requireGestureRecognizerToFail, how do I access the swipe gesture recognizer of the scroll view to add as an argument to this method? I know how to access scrollView.gestureRecognizers, but that doesn't help.jac300

1 Answers

2
votes

Th scroll view has a panGestureRecognizer property that you can access to establish the requireGestureRecognizerToFail relationship with your new tap gesture. Once you add the new tap gesture all of your tap processing would move from the current delegate method.

You should also implement gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: to allow all of your recognisers to operate at the same time.