Craa28
Craa28

Reputation: 1

Changing the image of a dequeued annotationview causes the image from the previous use to appear briefly

I am writing an app that displays a set of thumbnail images on a map. When I need to display a new thumbnail I dequeue a map annotationview using dequeueReusableAnnotationViewWithIdentifier. I then change the annotationview image. There appears to be an animation effect that fades from the image that was used by the previous use of the annotationview to the newly assigned image. Is there an easy way to just display the new image without the animation.

In:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation)

I have tried:

MKAnnotationView(annotation: annotation, reuseIdentifier: nil)

i.e. create a new annotation view that will not be reused after it is scrolled off the screen or is removed by the app. This resolves the problem but annotation views will never be available for dequeueReusableAnnotationView. For performance reasons this is not recommended but it fixes my problem! Is this the only solution?

Upvotes: 0

Views: 332

Answers (1)

Gerd Castan
Gerd Castan

Reputation: 6849

In your MKAnnotationView subclass, you should have something like

override var annotation: MKAnnotation? {
    willSet {
        ...
    }
}

In case an MKAnnotationView instance is not needed any more, MapKit sets annotation = nil.

This is an excellent time in willSet to clean up the objct: set the image to nil, maybe set other properties that you use to nil also.

If you are doing it this way, there should be no image referenced that could be shown.

Upvotes: 0

Related Questions