Search code examples
swiftmkmapviewmapkit

Skipping Selection, and Going Directly to Dragging in MapKit Marker


UPDATED: I'm really asking the wrong question. The first tap selects the marker. What I want to do is skip past the selection phase, and start the drag immediately. The selection interrupts the long press event, and I just want the long press event to start the selection. I tried calling setDragState with "starting" as the state, but that doesn't work. The object forgets that it is in drag state, and you still need that second tap.

ORIGINAL:

This is an issue I've had for a while. I have a draggable marker in an MKMapView object.

However, it always takes two (2) touches on the marker the first time before I can drag it. Once this has been done once, in subsequent drags, the first touch initiates drags immediately. It's only the first drag that requires the second touch.

It's not the end of the world, but it is slightly annoying.

Any ideas on how to make it so that the first touch also becomes the drag touch?


Solution

  • OK. I figured out how to do this.

    I "pre-select" the marker. This means that I can't do a simple callout for it, which, in my case is no big deal, but it gives me what I want.

    UPDATE: Code or it didn't happen.

    I added a couple of MKMapViewDelegate functions:

    /* ################################################################## */
    /**
     This responds to the map's region being changed.
     We simply use this to "preselect" the marker, so there's no need for two taps.
    
     - parameter mapView: The MKMapView object that contains the marker being moved.
     - parameter animated: True, if the change was animated.
     */
    func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
        self.mapView.selectAnnotation(self._meetingMarker, animated: false)
    }
    
    /* ################################################################## */
    /**
     This responds to the marker's selection turning off.
     We simply use this to "preselect" the marker, so there's no need for two taps.
    
     - parameter mapView: The MKMapView object that contains the marker being moved.
     - parameter didDeselect: The annotation view (it's ignored. We always select our marker).
     */
    func mapView(_ mapView: MKMapView, didDeselect view: MKAnnotationView) {
        self.mapView.selectAnnotation(self._meetingMarker, animated: false)
    }
    

    The first is called when I set up the map, and the second is called whenever something tries to turn off the selection.

    NOTE: This is only good for a map with a single marker.