Disable *some* gestures in a UIScrollView?

Discussion in 'iOS Programming' started by dantastic, Jul 1, 2011.

  1. dantastic macrumors 6502

    dantastic

    Joined:
    Jan 21, 2011
    #1
    I'm looking for some input here, what's the best way to go about this.

    I have a slideshow controller, a bunch of UIImageViews along a UIScrollView.

    I can scroll right and left using swipes but when I single tap I start a NSTimer based slideshow that scrolls the view so much every repeat. another single tap invalidates the timer.

    The problem is that the user can still swipe the screen and it messes up the slideshow animation.

    I need to be able to discard any gesture that is not a tap. I can't disable userinteraction, I can't disable scrolling.

    I have tried to disable all gestures that aren't tap recognizers
    Code:
    for (UIGestureRecognizer * gestureRecognizer in [self.scrollView gestureRecognizers]) {
    		if (![gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) {
    			[gestureRecognizer setEnabled:NO];
    		}
    	}
    
    I have tried to create a generic gesture and have that fail all but taps
    Code:
    for (UIGestureRecognizer * gestureRecognizer in [self.scrollView gestureRecognizers]) {
    		if (![gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]]) {
    			[gestureRecognizer requireGestureRecognizerToFail:self.gesture];
    		}
    	}
    
     
  2. nickculbertson macrumors regular

    nickculbertson

    Joined:
    Nov 19, 2010
    Location:
    Nashville, TN
    #2
    This might do the trick.

    Code:
    //in your single tap method
    scrollView.scrollEnabled = NO;
    
    //in your timer invalidate method
    scrollView.scrollEnabled = YES;
    
    Nick
     
  3. dantastic, Jul 2, 2011
    Last edited: Jul 2, 2011

    dantastic thread starter macrumors 6502

    dantastic

    Joined:
    Jan 21, 2011
    #3
    Nick, I haven't actually tested that one. I saw it on the auto-complete list but didn't want to test it as it sounded like it would disable scrolling completely and my slideshow is scrolling programmatically so that would have been silly. :)
    After you posted this though I had a look at the documentation and it clearly says that it disables touches.

    I will have to try this. thanks!

    EDIT:
    Works! That's exactly what I was looking for!
     

Share This Page