2012-10-17 68 views
1

在我的iPhone应用程序的其中一个UIViewControllers上,我附加了UIPanGestureRecognizer,以便当用户向左或向右滑动时,应用程序前进或返回一个屏幕。但是,问题在于,当用户在屏幕上点击(而不是滑动)时,它仍然在前进。我怎样才能阻止这种情况发生。我已粘贴以下相关代码:如何阻止UIPanGestureRecognizer识别水龙头

-(void) addGestureRecognizer 
{ 
    UIPanGestureRecognizer *pan; 
    pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRecognized:)]; 
    [pan setMinimumNumberOfTouches:1]; 
    [self.view addGestureRecognizer:pan]; 
} 

-(void) swipeRecognized: (UIPanGestureRecognizer *) recognizer 
{ 
    if(recognizer.state != UIGestureRecognizerStateBegan) return; 
    CGPoint velocity = [recognizer velocityInView:self.view]; 
    if(velocity.x > 0) 
    { 
     [self.navigationController popViewControllerAnimated:YES]; 
    } 
    else 
    { 
     @try 
     { 
      [self performSegueWithIdentifier:NEXT_STEP_SEGUE sender:self]; 
     } 
     @catch (NSException *exception) 
     { 
      //Silently die...muhaha 
     } 
    } 
} 

回答

1

我建议使用UISwipeGestureRecognizer进行刷卡。你有使用平底锅的特殊原因吗?

随着UISwipeGestureRecognizer你可以指定它应该识别手势的方向。

对于您的用户来说,使用适当的手势也更好。这样,他们会感觉在家里:)

+0

我正在使用它,以便我可以检测到1或2个手指轻扫以及两个方向上的轻扫 – Nosrettap

+0

UISwipeGestureRecognizers也有一个名为'numberOfTouchesRequired'的属性。你可以轻松地添加两个识别器,每个方向一个。我真的会催促你使用适当的手势。在你的解决方案中,移动1毫米的手指会触发平底锅,但这不会是一个滑动... – fguchelaar

+0

UISwipeGestureRecognizer和UIPanGestureRecognizer之间的区别究竟是什么? – Nosrettap

0

您应该使用fguchelaar建议的UISwipeGestureRecognizer,或使用translationInView:方法来确定用户实际移动手指的距离(对于水龙头,应该接近于零)。

如果状态不是UIGestureRecognizerStateBegan(方法中的第一行),您也不应该早返回,否则只有在手势开始时,您的方法才会被调用一次,但手势过程中不会。如果这就是你真正想要的,那么你只需要一个UISwipeGestureRecognizer。平移手势识别器的好处主要在于您可以跟踪用户的手指并提供直接反馈(例如在手指仍然向下时移动视图)。

相关问题