I added a swipe gesture recognizer and a pan gesture recognizer to the same view. These gestures should be exclusive to each other.
In order to do this I added the constraint on the swipe gesture
[swipeGesture requireGestureToFail:panGesture];
(because the pan gesture should get precedence)
Problem is that the pan gesture is always invoked - even during a very fast swipe.
In order to over come this I set myself as the pan gesture's delegate. In the delegate method I set up some code as follows:
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
// check if it is the relevant view
if (gestureRecognizer.view == self.myViewWithTwoGestures)
{
// check that it is the pan gesture
if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]])
{
UIPanGestureRecognizer *pan = (UIPanGestureRecognizer *)gestureRecognizer;
CGPoint velocity = [pan velocityInView:gestureRecognizer.view];
// added an arbitrary velocity for failure
if (ABS(velocity.y) > 100)
{
// fail if the swipe was fast enough - this should allow the swipe gesture to be invoked
return NO;
}
}
}
return YES;
}
Is there a suggested velocity to ensure good behavior? Is there another way to force the pan gesture to fail?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…