3

Well, I have some code to add 4 recognizers to a view, like this

- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib
    for(int d = UISwipeGestureRecognizerDirectionRight; d <= UISwipeGestureRecognizerDirectionDown; d = d*2) {
         UISwipeGestureRecognizer *sgr = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
         sgr.direction = d;
    [self.view addGestureRecognizer:sgr];
    }
    [self restore];
    [self populate];
    [self displaymap];

}

and a recognizer like this

-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer
{
printf("Guesture: %d\n", recognizer.direction);
if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft)
{
    printf("a\n");
    [self move: 'a'];
}
else if (recognizer.direction == UISwipeGestureRecognizerDirectionRight)
{
    printf("d\n");
    [self move: 'd'];
}
else if (recognizer.direction == UISwipeGestureRecognizerDirectionUp)
{
    printf("w\n");
    [self move: 'w'];
}
else if (recognizer.direction == UISwipeGestureRecognizerDirectionDown)
{
    printf("s\n");
    [self move: 's'];
}
else if (recognizer.direction == (UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionUp))
{
    printf("y\n");
    [self move: 'd'];
}
}

The problem is, it never detects the up | right direction, anybody know a way to fix this?

phyrrus9
  • 1,441
  • 11
  • 26

1 Answers1

5

That’s not how the direction property works. UISwipeGestureRecognizer only recognizes swipes in a single direction at a time. You’ll need to do something more complicated involving a UIPanGestureRecognizer and determining its direction from the result of its -translationInView: / -velocityInView: methods.

Noah Witherspoon
  • 57,021
  • 16
  • 130
  • 131
  • I am a little bad with programatically adding recognizers, do you think you could get me started? – phyrrus9 Dec 13 '13 at 00:24
  • Okay, I got one functional, am I supposed to be dividing the translationInView and velocityInView or is the / used as a "use this or that"? – phyrrus9 Dec 13 '13 at 00:51
  • “use this or that”. `-velocityInView:` is probably the better option—as soon as its magnitude goes above a certain threshold you can trigger the gesture in the direction described by that vector. – Noah Witherspoon Dec 13 '13 at 00:55
  • Thank you, works perfect. I actually checked the state and used velocity – phyrrus9 Dec 13 '13 at 01:06