Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two finger swipe in UIScrollview for iPad application

Actually i want to implement swipe left and right in UIScrollview. i have scrollview with content size (768,1500). i have tried this but problem is that sometimes its not detecting swipe and perform scrolling there. so now i want to disable scrolling on 2 finger touch.

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 have tried custom scrollview for that but i have problem with touchesBegan method. its not calling every time. even i tried this but not able to stop two finger scroll 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 that.

like image 375
Paras Gandhi Avatar asked Jan 03 '12 11:01

Paras Gandhi


1 Answers

I had the same problem; I needed to disable two-finger scrolling so that I could detect a two-finger swipe to the left or right. Here's what I did to set up my scroll view:

- (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
    }
}
like image 130
Jake Stoeffler Avatar answered Nov 15 '22 19:11

Jake Stoeffler