MKMapView unresponsive after long press

Hi!


I'm running into an issue I don't quite understand. I'm using an `MKMapView` with a `UILongPressGestureRecognizer`. The problem is that every first touch after a long press seems to be missed or ignored.


I've created a tiny little demo view controller in which it's reproducible (I've inlined the code in the post down below):


https://pastebin.com/JLBQwt7w


After the long press target is executed. It first takes a touch on the map view for it to start recognizing gestures again (not just long presses, also all standard map interactions like pan, tilt, zoom, etc.).


If I run this exact same code with a different kind of view (a standard `UIView` instead of the `MKMapView` for example), there's no problem.


Does anyone have a clue what's going on here?


(inline code sample in case that's easier)


import MapKit
import UIKit


class MainController: UIViewController {
    private let label = UILabel()
    private var count = 0


    override func viewDidLoad() {
        view.backgroundColor = .white


        let mapView = MKMapView()
        mapView.translatesAutoresizingMaskIntoConstraints = false


        label.textAlignment = .center
        label.translatesAutoresizingMaskIntoConstraints = false


        view.addSubview(mapView)
        view.addSubview(label)


        NSLayoutConstraint.activate([
            mapView.topAnchor.constraint(equalTo: view!.topAnchor),
            mapView.leftAnchor.constraint(equalTo: view!.leftAnchor),
            mapView.rightAnchor.constraint(equalTo: view!.rightAnchor),
            mapView.bottomAnchor.constraint(equalTo: label.topAnchor),
            label.leftAnchor.constraint(equalTo: view!.leftAnchor),
            label.rightAnchor.constraint(equalTo: view!.rightAnchor),
            label.bottomAnchor.constraint(equalTo: view!.bottomAnchor),
            label.heightAnchor.constraint(equalToConstant: 50),
        ])


        let gestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(handleMapPress(sender:)))
        gestureRecognizer.minimumPressDuration = 0.3


        mapView.addGestureRecognizer(gestureRecognizer)
    }


    @objc
    private func handleMapPress(sender: UIGestureRecognizer) {
        guard sender.state == .ended else {
            return
        }


        count += 1
        label.text = "Presses: \(count)"
    }
}

Don't know if you ever found a solution to this. I was facing the same issue and the only way round it that I could find was to manipulate the map in some way... for example, in my case, I was using a UILongPressGestureRecognizer to add an annotation so I centered the map on the new annotation coordinate:


@objc private func handleLongPressGesture(_ gesture: UILongPressGestureRecognizer) {

if gesture.state == .began {

let touch: CGPoint = gesture.location(in: self.map)

let coordinate: CLLocationCoordinate2D = self.map.convert(touch, toCoordinateFrom: self.map)


self.map.setCenter(coordinate, animated: true)

}

}


But centering the map on it's current center coordinate also works:


self.map.setCenter(self.map.centerCoordinate, animated: false)

Thank you!

Facing the same problem, your workaround worked for me!

thanks!


will be intersting find and fix the real problem

This is still a problem with iOS 14.4. Even the Apple-supplied Maps app demonstrates the behavior.

Centering the map on it's current center coordinate just worked in my app!

I solved it by registering as the delegate for the long press and implementing func gestureRecognizer(UIGestureRecognizer, shouldRecognizeSimultaneouslyWith: UIGestureRecognizer) -> Bool. That allows it to function as expected but not block gestures on the map.

MKMapView unresponsive after long press
 
 
Q