我有以下层次结构:
每个视图都分配了UIPanGestureRecognizer
。外部平移手势识别器仅对垂直平移感兴趣,因此我实现了委托方法:
- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)panGestureRecognizer {
CGPoint velocity = [panGestureRecognizer velocityInView:panGestureRecognizer.view];
return fabs(velocity.y) > fabs(velocity.x);
}
然而,Subview的平移手势始终优先于识别。有没有办法让外部手势识别器首先识别,然后如果失败,从子视图中平移手势可以接管吗?
我尝试了同时识别并且它有效,但是两个识别器都开始淘汰:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
答案 0 :(得分:3)
自iOS 7起,UIGestureRecognizerDelegate协议已经为您的需要声明了两种委托方法,您可以使用 | gestureRecognizer:shouldRequireFailureOfGestureRecognizer: | 要么 | gestureRecognizerShouldBegin:shouldBeRequiredToFailByGestureRecognizer: |但反过来说。
以下代码段是我的UIPanGestureRecognizer委托实现:
- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)panGestureRecognizer {
CGPoint velocity = [panGestureRecognizer velocityInView:panGestureRecognizer.view];
return fabs(velocity.y) > fabs(velocity.x);
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
if (otherGestureRecognizer == panGestureOfSubView) {
return YES;
}
return NO;
}
答案 1 :(得分:0)
此代码可能会有所帮助。这是我写的允许拖动特定视图的内容,但不是它的子视图:
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
if gestureRecognizer == longTouchGesture && otherGestureRecognizer == panTouchGesture && gestureRecognizer.view?.hitTest(gestureRecognizer.locationInView(gestureRecognizer.view), withEvent: nil) == self {
print("returning true")
return true
}
print("returning false")
return false
}