Note that there are some explanatory texts on larger screens.

plurals
  1. POSetting direction for UISwipeGestureRecognizer
    text
    copied!<p>I want to add simple swipe gesture recognition to my view based iPhone project. Gestures in all directions (right, down, left, up) should be recognized.</p> <p>It is stated in the docs for UISwipeGestureRecognizer:</p> <blockquote> <p>You may specify multiple directions by specifying multiple UISwipeGestureRecognizerDirection constants using bitwise-OR operands. The default direction is UISwipeGestureRecognizerDirectionRight.</p> </blockquote> <p>However for me it doesn't work. When all four directions are OR'ed only left and right swipes are recognized.</p> <pre><code>- (void)viewDidLoad { UISwipeGestureRecognizer *recognizer; recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)]; [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionUp)]; [[self view] addGestureRecognizer:recognizer]; [recognizer release]; [super viewDidLoad]; } -(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer { NSLog(@"Swipe received."); } </code></pre> <p>I fixed this with adding four recognizers to the view but I'm curious to know why didn't it work as advertised in docs?</p> <pre><code>- (void)viewDidLoad { UISwipeGestureRecognizer *recognizer; recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)]; [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)]; [[self view] addGestureRecognizer:recognizer]; [recognizer release]; recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)]; [recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)]; [[self view] addGestureRecognizer:recognizer]; [recognizer release]; recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)]; [recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)]; [[self view] addGestureRecognizer:recognizer]; [recognizer release]; recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)]; [recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)]; [[self view] addGestureRecognizer:recognizer]; [recognizer release]; [super viewDidLoad]; } -(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer { NSLog(@"Swipe received."); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload