简体   繁体   中英

uipangesturerecognizer with uiswipegesturerecognizer

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];
}

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");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM