我有以下层次结构:
每个 View 都分配了 UIPanGestureRecognizer 。外部平移手势识别器只对垂直平移感兴趣,所以我为此实现了委托(delegate)方法:
- (BOOL)gestureRecognizerShouldBeginUIPanGestureRecognizer *)panGestureRecognizer {
CGPoint velocity = [panGestureRecognizer velocityInView:panGestureRecognizer.view];
return fabs(velocity.y) > fabs(velocity.x);
}
但是,Subview 的平移手势在识别中始终优先。有没有办法让外部平移手势识别器先识别,如果失败, subview 的平移手势可以接管?
我尝试了同时识别并且它可以工作,但是两个识别器都开始平移:
- (BOOL)gestureRecognizerUIGestureRecognizer *)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizerUIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
Best Answer-推荐答案 strong>
从 iOS 7 开始,UIGestureRecognizerDelegate 协议(protocol)已经声明了两个代理方法来满足你的需要,你可以使用
|gestureRecognizer:shouldRequireFailureOfGestureRecognizer:|
或者
|gestureRecognizerShouldBegin:shouldBeRequiredToFailByGestureRecognizer:|但相反。
以下片段是我的 UIPanGestureRecognizer 委托(delegate)实现:
- (BOOL)gestureRecognizerShouldBeginUIPanGestureRecognizer *)panGestureRecognizer {
CGPoint velocity = [panGestureRecognizer velocityInView:panGestureRecognizer.view];
return fabs(velocity.y) > fabs(velocity.x);
}
- (BOOL)gestureRecognizerUIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizerUIGestureRecognizer *)otherGestureRecognizer{
if (otherGestureRecognizer == panGestureOfSubView) {
return YES;
}
return NO;
}
关于ios - 解决两个平移手势识别器之间的冲突,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/29233465/
|