ios - UIGestureRecognizer.State 是 'possible' 而不是 'recognized' 每隔一次点击 MKMapView

标签 ios swift xcode mkmapview uigesturerecognizer

我正在尝试了解我的手势识别器的可重现错误。我在 MKMapView 上有 2 个识别器,一个 UITapGestureRecognizer 和一个 UILongPressGestureRecogniser。它们都按预期第一次工作,但是,如果我使用长按(这会向 map 添加注释),下一个点击手势将返回“可能”状态,但永远不会达到“已识别”状态。

▿ 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>

在我点击一次后没有任何反应,第二次点击将执行关联的功能,即使其进入识别状态。

我拦截了窗口上的所有点击,每次点击肯定都会发生,但长按后的第一个点击似乎从未被接受。我在这里缺少什么吗?添加的手势如下:

let mapTap = UITapGestureRecognizer(target: self, action: #selector(mapTapped(_:)))
        mapView.addGestureRecognizer(mapTap)

let pressGesture = UILongPressGestureRecognizer(target: self, action: #selector(mapLongPress(_:)))
            pressGesture.minimumPressDuration = 0.2
            pressGesture.numberOfTouchesRequired = 1
            mapView.addGestureRecognizer(pressGesture)

这可能与默认添加到 MKMapView 的其他手势有关吗?

最佳答案

我尝试使用您的代码并得到相同的结果。

我用一个棘手的解决方案解决了它。希望对你有所帮助

 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
 }

关于ios - UIGestureRecognizer.State 是 'possible' 而不是 'recognized' 每隔一次点击 MKMapView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64208905/

相关文章:

ios - Swift UIImage 转换为 PDF

ios - 当我从一个 View 导航到另一个 View 时,如何让音乐持续播放?

ios - 如何在 iOS 中播放 NSData 音频或视频?

ios - unity 5 "Build & Install iPhone player"对话框不消失

ios - 在 swift 中与 NSDateFormatter 作斗争

swift - 在 Swift 中将常量字符串转换为变量字符串

ios - isBatteryMonitoringEnabled 属性状态未变为 true

xcode - 使用 `expect` 滚动浏览并接受许可协议(protocol)

iOS 11 beta 9 空键盘按键

ios - 此操作无法完成。再试一次 (-22421) (Xcode)