在我的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或2个手指轻扫以及两个方向上的轻扫 – Nosrettap
UISwipeGestureRecognizers也有一个名为'numberOfTouchesRequired'的属性。你可以轻松地添加两个识别器,每个方向一个。我真的会催促你使用适当的手势。在你的解决方案中,移动1毫米的手指会触发平底锅,但这不会是一个滑动... – fguchelaar
UISwipeGestureRecognizer和UIPanGestureRecognizer之间的区别究竟是什么? – Nosrettap