Ios UIgestureRecognitor。状态为';可能';而不是';公认的';每隔单击一次MKMapView
我试图用我的手势识别器来理解一个可复制的错误。我在MKMapView上有两个识别器,一个UITapGestureRecognitor和一个UILongPressGestureRecognitor。但是,如果我第一次使用长按(在地图上添加注释),下一次点击手势将返回“可能”状态,但不会达到“已识别”状态Ios UIgestureRecognitor。状态为';可能';而不是';公认的';每隔单击一次MKMapView,ios,swift,xcode,mkmapview,uigesturerecognizer,Ios,Swift,Xcode,Mkmapview,Uigesturerecognizer,我试图用我的手势识别器来理解一个可复制的错误。我在MKMapView上有两个识别器,一个UITapGestureRecognitor和一个UILongPressGestureRecognitor。但是,如果我第一次使用长按(在地图上添加注释),下一次点击手势将返回“可能”状态,但不会达到“已识别”状态 ▿ Optional<Array<UIGestureRecognizer>> ▿ some : 2 elements - 0 : <UITapGestur
▿ Optional<Array<UIGestureRecognizer>>
▿ some : 2 elements
- 0 : <UITapGestureRecognizer: 0x7fda7543ebc0; state = Possible; view = <MKMapView 0x7fda78026e00>>
- 1 : <UILongPressGestureRecognizer: 0x7fda7543e8c0; state = Possible; view = <MKMapView 0x7fda78026e00>; numberOfTapsRequired = 0; minimumPressDuration = 0.2>
这是否与默认情况下在MKMapView上添加的其他手势有关?在您的情况下,您希望点击识别器和长按识别器同时工作:当您点击视图时,两者都应启动识别过程。当您在长按的最短轻触时间之前结束轻触时,轻触手势应触发,但当您稍后结束轻触时,长按手势应触发。
但政府说: UIKit通常只允许一次识别一个手势 单一视图。一次只识别一个手势通常是错误的 更可取,因为它可以防止用户输入触发超过 一次一个动作。但是,此默认行为可能会引入 意外的副作用。例如,在同时包含平移和平移的视图中 和刷卡手势识别器,刷卡永远不会被识别。因为 平移手势识别器是连续的,它始终能够识别其 滑动手势识别器之前的手势,这是离散的 在您的情况下,长点击手势识别器是连续的,而点击手势识别器是离散的,因此识别点击可能存在问题 因此,我将尝试明确地允许两个识别器同时识别他们的手势。下面给出了一个如何实现这一点的示例。
长按识别器一启动,您就可以取消轻触识别器的识别操作。
希望这有帮助 我尝试使用你的代码,得到了相同的结果 我用一个棘手的办法解决了它。我希望这对你有帮助
mapTap = UITapGestureRecognizer(target: self, action: #selector(mapTapped(_:)))
mapTap.delegate = self
mapView.addGestureRecognizer(mapTap)
pressGesture = UILongPressGestureRecognizer(target: self, action:
#selector(mapLongPress(_:)))
pressGesture.minimumPressDuration = 0.2
pressGesture.numberOfTouchesRequired = 1
mapView.addGestureRecognizer(pressGesture)
@objc func mapTapped(_ gesture: UITapGestureRecognizer) {
// your code
}
@objc func mapLongPress(_ gesture: UILongPressGestureRecognizer) {
// your code
if gesture.state == .began {
mapTap.isEnabled = false
} else if gesture.state == .cancelled || gesture.state == .ended {
mapTap.isEnabled = true
}
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
嗯,这可能行得通,这不是最理想的解决方案,因为您必须保持启用状态,但这肯定是一个解决方案,可能是一个很好的临时解决方案。谢谢你的回答是的,这是有道理的,我想我有点困惑苹果地图如何处理长按/点击手势,以及许多其他手势识别器。这似乎与上述说法有点矛盾。我们将看看上面的解决方案,看看它是否提供了更好的解决方案,谢谢您的回复。
mapTap = UITapGestureRecognizer(target: self, action: #selector(mapTapped(_:)))
mapTap.delegate = self
mapView.addGestureRecognizer(mapTap)
pressGesture = UILongPressGestureRecognizer(target: self, action:
#selector(mapLongPress(_:)))
pressGesture.minimumPressDuration = 0.2
pressGesture.numberOfTouchesRequired = 1
mapView.addGestureRecognizer(pressGesture)
@objc func mapTapped(_ gesture: UITapGestureRecognizer) {
// your code
}
@objc func mapLongPress(_ gesture: UILongPressGestureRecognizer) {
// your code
if gesture.state == .began {
mapTap.isEnabled = false
} else if gesture.state == .cancelled || gesture.state == .ended {
mapTap.isEnabled = true
}
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}