Two finger swipe in UIScrollview app for iPad

Actually, I want to implement left and right scroll in UIScrollview. I have a scrollview with content size (768,1500). I tried this, but the problem is that sometimes it does not detect swipe and scrolls there. so now I want to disable 2-finger scrolling.

swipeGesture = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(nextswipedScreen:)] autorelease]; swipeGesture.numberOfTouchesRequired=2; swipeGesture.direction = UISwipeGestureRecognizerDirectionLeft; [self addGestureRecognizer:swipeGesture]; swipeGesture = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(previousswipedScreen:)] autorelease]; swipeGesture.numberOfTouchesRequired=2; swipeGesture.direction = UISwipeGestureRecognizerDirectionRight; [self addGestureRecognizer:swipeGesture]; 

I tried a custom scrollview for this, but I have a problem with the touchhesBegan method. not calling him every time. even I tried this but couldn't stop two-finger scrolling in UIScrollview.

 for (UIGestureRecognizer *mgestureRecognizer in _scrollView.gestureRecognizers) { if ([mgestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) { UIPanGestureRecognizer *mpanGR = (UIPanGestureRecognizer *) mgestureRecognizer; mpanGR.minimumNumberOfTouches = 1; mpanGR.maximumNumberOfTouches = 1; } } 

Let me know if you have any solution or alternative for this.

+8
iphone uiscrollview uigesturerecognizer
source share
2 answers

I had the same problem; I needed to disable two-finger scrolling so that I could detect two fingers left or right. Here is what I did to customize the scroll:

 - (void) setUpGestureHandlersOnScrollView:(UIScrollView *)scrollView { // set up a two-finger pan recognizer as a dummy to steal two-finger scrolls from the scroll view // we initialize without a target or action because we don't want the two-finger pan to be handled UIPanGestureRecognizer *twoFingerPan = [[UIPanGestureRecognizer alloc] init]; twoFingerPan.minimumNumberOfTouches = 2; twoFingerPan.maximumNumberOfTouches = 2; [scrollView addGestureRecognizer:twoFingerPan]; // set up the two-finger left and right swipe recognizers UISwipeGestureRecognizer *twoFingerSwipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleGestureFrom:)]; twoFingerSwipeLeft.direction = UISwipeGestureRecognizerDirectionLeft; twoFingerSwipeLeft.numberOfTouchesRequired = 2; [scrollView addGestureRecognizer:twoFingerSwipeLeft]; UISwipeGestureRecognizer *twoFingerSwipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleGestureFrom:)]; twoFingerSwipeRight.direction = UISwipeGestureRecognizerDirectionRight; twoFingerSwipeRight.numberOfTouchesRequired = 2; [scrollView addGestureRecognizer:twoFingerSwipeRight]; // prevent the two-finger pan recognizer from stealing the two-finger swipe gestures // this is essential for the swipe recognizers to work [twoFingerPan requireGestureRecognizerToFail:twoFingerSwipeLeft]; [twoFingerPan requireGestureRecognizerToFail:twoFingerSwipeRight]; } 

The handler method should look something like this:

 - (void)handleGestureFrom:(UISwipeGestureRecognizer *)recognizer { if ([recognizer numberOfTouches] == 2) { // do whatever you need to do } } 
+22
source share

You can create two gesture recognizers: one for one tap and one for double tap:

 UITapGestureRecognizer *singleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTouchesOne:)]; singleTapGesture.cancelsTouchesInView = NO; singleTapGesture.delaysTouchesEnded = NO; singleTapGesture.numberOfTouchesRequired = 1; // One finger single tap singleTapGesture.numberOfTapsRequired = 1; [Scroll_view addGestureRecognizer:singleTapGesture]; [singleTapGesture release]; UITapGestureRecognizer *doubleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTouchesTwo:)]; doubleTapGesture.cancelsTouchesInView = NO; doubleTapGesture.delaysTouchesEnded = NO; doubleTapGesture.numberOfTouchesRequired = 1; // One finger double tap doubleTapGesture.numberOfTapsRequired = 2; [Scroll_view addGestureRecognizer:doubleTapGesture]; [doubleTapGesture release]; 

And now, here comes the blow:

 [singleTapGesture requireGestureRecognizerToFail : doubleTapGesture]; 

requireGestureRecognizerToFail Reference

The last line makes your single descriptor a handler only if the double tap fails. This way you get both a single tap and a double tap in your application.

And you can do as in the "doubleTapGesture" method you specified only scrolling with the content size (0,0). In the "singleTapGesture" method, you specified scrolling with the content size (768,1500).

Sources of knowledge

+1
source share

All Articles