iPhone simulator swipe right works, but swipe left does not?
Asked Answered
F

3

14

I'm trying to use swipe left and right on a UIScrollView. However it looks like swipe left does not work in iPhone simulator even though swipe right does. Did I miss any step?

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    self.scrollView.multipleTouchEnabled = YES;
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
    swipe.delaysTouchesBegan = YES;
    swipe.numberOfTouchesRequired = 2;
    [self.scrollView addGestureRecognizer:swipe];
    [swipe release];
}

- (void)handleSwipe:(UISwipeGestureRecognizer *)recognizer
{
    if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft) {

    } else if (recognizer.direction == UISwipeGestureRecognizerDirectionRight) {

    }
}
Faludi answered 5/7, 2011 at 3:41 Comment(0)
A
19

Use Following:

UISwipeGestureRecognizer *rightRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwipeHandle:)];
rightRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
[rightRecognizer setNumberOfTouchesRequired:1];
[mainSlideShowImageScrollView addGestureRecognizer:rightRecognizer];
[rightRecognizer release];
UISwipeGestureRecognizer *leftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwipeHandle:)];
leftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
[leftRecognizer setNumberOfTouchesRequired:1];
[mainSlideShowImageScrollView addGestureRecognizer:leftRecognizer];
[leftRecognizer release];   



- (void)rightSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer 
   {
      //Do moving
   }

- (void)leftSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer 
{
  // do moving
}
Alpenhorn answered 5/7, 2011 at 3:49 Comment(3)
You mean creating 2 UISwipeGestureRecognizers, one with selector rightSwipeHandle:, the other with leftSwipeHandle: ? Do I still need to check the recognizer direction inside those 2 handlers?Faludi
Yes I meant so. And you don't need to bother for any thing else, just to what you want to do, in left or right swipe. But yeah don't miss the code written initially, as it gesture recognizer to a particular container.Alpenhorn
Just noticed that I can set UISwipeGestureRecognizer.direction in the viewWillAppear. Swipe left works after I set the direction to UISwipeGestureRecognizerDirectionLeft. Now I understand what you mean. Thank you very much.Faludi
R
4

Your 'handleSwipe' code is actually correct. You need two UISwipeGestureRecognizers but you can point them both to the same handler, containing your 'IF' statement.

Retrorse answered 23/12, 2011 at 11:35 Comment(0)
S
0

You can create one gesture recognizer that handles both left and right swipe (or even all directions-!):

    UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
        swipeGesture.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight;

All directions:

  swipeGesture.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown;
Savonarola answered 11/3, 2016 at 10:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.