Search code examples
iphoneiosipaduiswipegesturerecognizer

UISwipeGestureRecognizerDirection issue


In viewDidLoad I set:

    UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
    [swipeGesture setDirection:(UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight)];
    [self.view addGestureRecognizer:swipeGesture];

-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
    NSLog(@"Swipe received.");
    UISwipeGestureRecognizerDirection temp = recognizer.direction;
    if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft)
    {
        [self backCalendarPressed:nil];
    }
    else if (recognizer.direction == UISwipeGestureRecognizerDirectionRight)
    {
        [self nextCalendarPressed:nil];
    }
}

but recognizer.direction is always equal to '3'. And that's why I can't determine if it's left or right swipe.


Solution

  • you have to set a separate gesture recognizer for each direction if you want to discern between left and right gestures. the direction property only gives you what you set as allowed direction (3 = both directions). You can give the same target method to both, and in that method ask for the direction of the recognizer as you are doing.