Search code examples
iosswiftmkmapview

Disable DoubleTapGesture Zoom out on MKMapView


I have observed MKMapView zoom-out on Double tap gesture, I didn't find any way to disable it. I tried to add own Double Tap Gesture to catch Double tap action but it still zoom out. Any thought?


Solution

  • There is no API to disable or change double-tap behavior with MKMapView.

    But, if you really want to do so, one approach would be to find and remove the double-tap gesture recognizer from the MKMapView object.

    In the project you shared, you could do that in makeUIView in your UIMapView class:

    func makeUIView(context: UIViewRepresentableContext<UIMapView>) -> UIViewType {
        self.configureView(mapView, context: context)
        setRegion(to: palce)
        
        if let v = mapView.subviews.first,
           let ga1 = v.gestureRecognizers
        {
            let ga2: [UITapGestureRecognizer] = ga1.compactMap { $0 as? UITapGestureRecognizer } .filter { ($0.numberOfTapsRequired == 2) }
            for g in ga2 {
                v.removeGestureRecognizer(g)
            }
        }
    
        return mapView
    }
    

    I wouldn't necessarily suggest that you do so, however.

    • Apple may change the MKMapView object in the future, which could then break this.
    • User's tend to prefer that common UI elements behave in expected ways.

    Personally, I get rather annoyed when using an app and the developer has changed standard functionality of UI elements. For example, if I see a table view row with a disclosure indicator (the right-arrow / chevron), I expect that tapping the row will "push" to another screen related to that row. I've seen apps that do not follow that pattern, and it just gets confusing.