iPhone - 与多个 UIGestureRecognizers 冲突
我目前与 UIGestureRecognizers 存在一些冲突,这导致一切都彼此很好地放置在一起。我在屏幕上有几个正方形(UIView),可以让用户平移和捏合(用于缩放视图)。我在主视图中添加了一个 UIPinchGestureRecognizer,其中添加了正方形,以便我可以缩放焦点中的正方形。我还向每个方块添加了 UIPanGestureRecognizers,以便它可以在屏幕中移动。当我捏缩放选定的方块,同时手指在其他方块上移动时,问题就会显现出来。根据我的调试,似乎如果我的捏合手指穿过非聚焦方块,它们就会吃掉抵消捏合手势的触摸。使用“[pan requireGestureRecognizerToFail:pinch]”给予捏合优先级,但会创建并发出问题,因为连续平移识别器不再触发。我还尝试将 UIPinchRecognizer 直接添加到正方形,但它可以工作,但手势具有在正方形范围内的约束,如果正方形缩小太多,则效果不佳。有办法解决这个问题吗?我是不是错过了什么?
I'm currently having some conflicts with UIGestureRecognizers that is causing everything to place nice with each other. I have several squares (UIView) on the screen that let the user to pan and pinch (used to scale the views). I have a UIPinchGestureRecognizer added to the main view which the squares are added so that I can scale the square in focus. I've also added UIPanGestureRecognizers to each square so that it can be moved around in the screen. The problem manifests itself when I pinch to scale a selected square while my fingers are moving across the others. Based on my debugging, it appears that if my pinching fingers go across the non focused squares they eat the touches which cancel out the pinching gesture. Using "[pan requireGestureRecognizerToFail: pinch]" give the pinch priority but creates and issue because the continuous pan recognizer no longer fires. I've also tried to add the UIPinchRecognizer directly to the square but which works but the gesture has the constraint of being within the bounds of the square which does not work well if the square is scaled down too much. Is there a way around this? I'm I missing something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决问题的一种方法是为所有 UIGestureRecognizers 设置一个公共委托(可能是此视图的 UIViewController)。如果捏合手势识别器处于“开始”或“更改”状态(意味着它正在识别和处理捏合),则该委托可以为gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer 返回NO。这应该可以防止任何平移手势识别器在捏合手势期间吃掉触摸。
在接口文件中,您需要保存对捏合手势识别器的引用:
并且在实现中,确保检查捏合手势识别器的状态,而不是正在传递的手势识别器的状态:
One way around your problem would be to set a single common delegate for all of your UIGestureRecognizers (probably the UIViewController for this view). That delegate could return NO for gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer if the pinch gesture recognizer was in the "Began" or "Changed" states (meaning it was recognizing and processing a pinch). That should prevent any of the pan gesture recognizers from eating touches during a pinch gesture.
In the interface file, you will need to save a reference to the pinch gesture recognizer:
And in the implementation, make sure you check the pinch gesture recognizer's state, not the state of the gesture recognizer being passed: