Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top