1

I have the following UIGestureRecognizer which detects swipe up properly

- (void) addGestureRecognizer {
    _swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)];
    [_swipeRecognizer setDirection:UISwipeGestureRecognizerDirectionUp];
    [_swipeRecognizer setDelegate:self];
    [self.view addGestureRecognizer:_swipeRecognizer];
}

- (void) didSwipe:(id)sender{
    NSLog(@"didSwipe");
}

I then modified the direction to include left and right

[_swipeRecognizer setDirection:UISwipeGestureRecognizerDirectionUp|UISwipeGestureRecognizerDirectionLeft|UISwipeGestureRecognizerDirectionRight];

and it no longer responds to swiping up (swiping left or right works). What am I doing wrong? (tried on simulator, iphone5, ipad3)

Note: I do not need to detect the actual direction of the swipe. I just need to know there is a swipe. Thanks.

minovsky
  • 857
  • 1
  • 15
  • 28
  • possible duplicate of [Recognize swipe in all 4 directions?](http://stackoverflow.com/questions/8181774/recognize-swipe-in-all-4-directions) – Pfitz Mar 20 '13 at 04:41
  • this (http://stackoverflow.com/a/3415323/1387720) is better than the linked post above. thanks – minovsky Mar 20 '13 at 04:53

2 Answers2

1

Try this way

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

    _swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)];
    [_swipeRecognizer setDirection:UISwipeGestureRecognizerDirectionUp|UISwipeGestureRecognizerDirectionDown];
    [_swipeRecognizer setDelegate:self];
    [self.view addGestureRecognizer:_swipeRecognizer];

EDIT

As @LearnCocos2D suggested " Apparently each UISwipeGestureRecognizer can only detect the swipe in the given direction. Even though the direction flags could be OR'ed together the UISwipeGestureRecognizer ignores the additional flags. "

And As per your "Note: I do not need to detect the actual direction of the swipe. I just need to know there is a swipe.", You just need to detect swipe not the direction so combining right-left as one and up-down as other gesture will work.

Community
  • 1
  • 1
βhargavḯ
  • 9,786
  • 1
  • 37
  • 59
  • this (http://stackoverflow.com/a/3415323/1387720) is a much better explanation than the one everyone seems to be referencing. thanks for your help though! – minovsky Mar 20 '13 at 04:56
0

swipeGesture is discree gesture, and swipe up and down is two different gesture. You must create 4 gesture and send it to one action.

- (void) addGestureRecognizer {
    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)];
    [swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
    [swipeLeft setDelegate:self];
    [self.view addGestureRecognizer:swipeLeft];

    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)];
    [swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
    [swipeRight setDelegate:self];
    [self.view addGestureRecognizer:swipeRight];

}

- (void) didSwipe:(UISwipeGestureRecognizer *)sender{
    NSLog(@"didSwipe");
}
LE SANG
  • 10,955
  • 7
  • 59
  • 78
  • that is not correct. `direction` is a bit mask. Check this answer. http://stackoverflow.com/questions/8181774/recognize-swipe-in-all-4-directions?rq=1 – Pfitz Mar 20 '13 at 04:41