uipangesturerecognizer 与 uiswipegesturerecognizer

uipangesturerecognizer with uiswipegesturerecognizer

目前使用 panGesture 时滑动并不完美。如果用户想向上滑动,但他向右滑动了一个像素,但其余部分向上滑动(用户向上滑动,但在开始时略微向右滑动),但右侧将被激活而向上不会。我认为集成 swipeGesture 可以最大限度地减少这种错误,但它似乎不起作用。我想要它,所以当用户在开始时没有完美地向上滑动时,向上仍然会被激活,而不是向左或向右。它确实可以向上、向左或向右移动,但它不是 100% 完美,因为用户只需更改 y 或 x 轴,这几乎是不可能的。

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

无需添加 UISwipeGestureRecognizer,您可以简单地使用 if 语句。如果他们先按 x 坐标 'by accident' 移动,您可以 运行 一个 if 语句,然后再说它是垂直的。将 10 更改为您喜欢的 x 坐标更改。

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