解决两个平移手势识别器之间的冲突
Resolve conflict between two pan gesture recognizers
我有以下层次结构:
- 查看
- 子视图
每个视图都已分配 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;
}
从iOS7开始,UIGestureRecognizerDelegate Protocol已经声明了两个委托方法来满足你的需要,你可以使用
|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;
}
这段代码可能会有帮助。这是我写的允许拖动特定视图,但不是它的子视图:
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
}
我有以下层次结构:
- 查看
- 子视图
每个视图都已分配 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;
}
从iOS7开始,UIGestureRecognizerDelegate Protocol已经声明了两个委托方法来满足你的需要,你可以使用 |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;
}
这段代码可能会有帮助。这是我写的允许拖动特定视图,但不是它的子视图:
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
}