Oscar TJ
Oscar TJ

Reputation: 534

Custom MKAnnotationView want to be tapped again

I'm having problems with my custom MKAnnotationView. I download the coordinate where to place it from my REST api and I place the pin on map with this code:

private func refreshMapView(andCenterMap: Bool){
    DispatchQueue.main.async { [weak self] in
        guard let self = self else {
            return
        }

        self.mapView.addAnnotations(self.spotsArray)
        if andCenterMap {
            self.centerMap(at: self.mapView.userLocation.coordinate)
        }
    }
}

Ones the pin is placed I zoom automatically the map.

Here the code for the custom annotation creation:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    // Do not touch the annotation of the user location
    if annotation.isKind(of: MKUserLocation.self){
        return nil
    }

    let annoIdentifier = "SPOT"
    var annotationView: MKAnnotationView?

    if let dequeued = mapView.dequeueReusableAnnotationView(withIdentifier: annoIdentifier) {
        annotationView = dequeued
    }else{
        let av = MKAnnotationView(annotation: annotation, reuseIdentifier: annoIdentifier)
        annotationView = av
    }

    // Changing the image of the pin
    annotationView!.annotation = annotation
    if let image = UIImage(named: "map_pin") {
        annotationView!.image = image
        let deltaY = image.size.height/2
        annotationView!.centerOffset = CGPoint(x: 0.0, y: -deltaY)
    }else{
        annotationView!.image = nil
    }

    return annotationView
}

As you can notice, my custom pin is using this image (@1x, @2x, @3x)

map_pin map_pin@2x map_pin@3x

Ones tapped I want to show the "detail view" and I use the annotation as a sender in order to have all the information I need on the next view. Here the code:

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
        guard let ann = view.annotation else {
            return
        }
        if ann.isKind(of: MKUserLocation.self){
            return
        }
        self.performSegue(withIdentifier: "showDetailSegue", sender: ann)
}

So I present all the data I need to and I can go back to the "map view".

The problem is:

In the map view, if I want to select the same annotation again, this is not tappable anymore.

I can see it (in the right place) but I can select it again only if I zoom a little bit and I tap "around" the annotation.

Any suggestione how to solve this issue?

Thanks in advance!

Upvotes: 0

Views: 36

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100503

Add this end of didSelect

mapView.deselectAnnotation(ann,animated:false)

Upvotes: 1

Related Questions