我有一个带有子视图的UIScrollView实例,该子视图有自己的UIPanGestureRecognizer,用于在scrollview中移动子视图。我希望scrollview的pinch-to-zoom功能优先于平移手势。然而,事实并非如此:当我用一根手指捏住我的子视图时,它会反过来。
到目前为止我尝试了什么:
gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:
:它会平移并捏合[panGestureRecognizer requireGestureRecognizerToFail:scrollView.pinchGestureRecognizer];
:平移只是不再工作(仅针对UIGestureRecognizerStateEnded
状态触发回调)。 panGestureRecognizer.cancelsTouchesInView = NO
没有帮助。我的理解是,当开始从子视图中捏合时,滚动视图和子视图都只接受一次触摸,这就是为什么在第二种情况下捏合手势识别器甚至不会因为失败而它甚至没有开始处理这个事件。
那么,关于如何实现这个的任何想法?
答案 0 :(得分:3)
一种方法是允许与scrollView.pinchGestureRecognizer同时进行交互,然后在scrollView.pinchGestureRecognizer实际缩放时取消panGestureRecognizer。我知道如何取消它们的唯一方法是禁用/启用。
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
if (gestureRecognizer == panGestureRecognizer){
if (otherGestureRecognizer == scrollView.panGestureRecognizer) {
return 0;
}
else if(otherGestureRecognizer == scrollView.pinchGestureRecognizer){
if (scrollView.pinchGestureRecognizer.scale != 1) {
gestureRecognizer.enabled = 0;
gestureRecognizer.enabled = 1;
}
}
}
return 1;
}