uipangesturerecognizer with uiswipegesturerecognizer

145 views Asked by At

Currently with panGesture the swiping isn't perfect. If a user wants to swipe up but he goes one pixel to the right however the rest are up (user swipes up but ever so slightly goes to the right at the start), but the right will be activated and the up wouldn't. I thought of integrating swipeGesture would minimise this sort of error, however it seems to not work. I want it so when the user doesn't perfectly swipe Up at the start, up will still be activated and not left or right. It does work going up, down left or right but it's not 100% perfect as the user has to only change the y or x axis which is almost impossible.

-(void)pan:(UIPanGestureRecognizer *)sender
{
CGPoint distance = [sender translationInView:self.view];
UISwipeGestureRecognizer *swiping;
[myImage addGestureRecognizer: swiping];

//Not Working
if(swiping.direction == UISwipeGestureRecognizerLeft || swiping.direction == UISwipeGestureRecognizerRight) {
NSLog(@"Verticle")
} else if(swiping.direction == UISwipeGestureRecognizerUp || swiping.direction == UISwipeGestureRecognizerDown) {
NSLog(@"Horizontal");
} else if(distance.x > 0 || distance.x < 0) { // From here down working but not perfect
NSLog(@"Verticle")
} else if (distance.y < 0 || distance.y > 0) {
NSLog(@"Horizontal")
}
[sender setTranslation:CGPointZero inView:self.view];
}
1

There are 1 answers

0
Michael Mooney On BEST ANSWER

Rather than adding UISwipeGestureRecognizer, you could simply use an if statement. If they move by the x-coordinate 'by accident' first, you could run an if statement before saying it's vertical. Change the 10 to you liking on the x-coordinate change.

-(void)Pan: (UIPanGestureRecognizer *)sender
{
    distance = [sender translationInView:self.view];

    if (distance.x > 0 || distance.x < 0) {
        if ((distance.y > 0 || distance.y < 0) && ((distance.x > 0 && distance.x < 10) || (distance.x < 0 && distance.x > -10))) {
            NSLog(@"Horizontal");
        } else {
            NSLog(@"Verticle");
        }
    } else if (distance.y > 0 || distance.y < 0) {
        NSLog(@"Starting Up Horizontal");
    }
    [sender setTranslation:CGPointZero inView:self.view];

    if (sender.state == UIGestureRecognizerStateEnded) {
        NSLog(@"Ended");
    }
}