0

目前与panGesture刷卡并不完美。如果用户想要向上滑动,但是他向右移动一个像素,但其余的都已移动(用户向上滑动但在开始时略微向右移动),但是右侧将被激活,而右侧则不会。我认为集成swipeGesture会尽量减少这种错误,但它似乎不起作用。我想要它,所以当用户在开始时没有完全向上滑动时,向上仍然会被激活,而不是向左或向右。它可以向左或向右运动,但它不是100%完美的,因为用户只需要改变几乎不可能的y或x轴。uipangesturerecognizer与uiswipegesturerecognizer

-(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

与其添加UISwipeGestureRecognizer,您可以简单地使用if语句。如果他们先移动了X坐标'不小心',那么你可以运行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"); 
    } 
} 
相关问题