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) {

    }
}
Guoliang Cao
  • 507
  • 4
  • 11

3 Answers3

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
}
rptwsthi
  • 10,094
  • 10
  • 68
  • 109
  • 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? – Guoliang Cao Jul 05 '11 at 03:52
  • 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. – rptwsthi Jul 05 '11 at 03:56
  • 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. – Guoliang Cao Jul 05 '11 at 03:59
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.

Kokodoko
  • 26,167
  • 33
  • 120
  • 197
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;
LMVogel
  • 779
  • 7
  • 28