How to easily remove annotation from a map?

I add and remove map annotations. When I delete one, it suddenly disappears and looks a little startling, I would prefer it to gradually disappear.

I tried to remove it using UIView: animateWithDuration: but it was not an animating attribute.

If there is no other simple solution that I was thinking about, I could get an annotation view to fade out its alpha, and then remove its annotation from the map. However, the problem with this is that it doesn't seem like there is a link to its map view in the annotation view? Adding one starts to get a little confused. Is there a simple quick fix to remove annotations gracefully?

+4
source share
2 answers

Use animateWithDurationshould work fine. To disappear annotation deletion, you can:

MKAnnotationView *view = [self.mapView viewForAnnotation:annotation];

if (view) {
    [UIView animateWithDuration:0.5 delay:0.0 options:0 animations:^{
        view.alpha = 0.0;
    } completion:^(BOOL finished) {
        [self.mapView removeAnnotation:annotation];
        view.alpha = 1.0;   // remember to set alpha back to 1.0 because annotation view can be reused later
    }];
} else {
    [self.mapView removeAnnotation:annotation];
}
+6
source

I think your proposed solution is correct. Set the animation to opacity = 0, then, when finished, remove the annotation from MKMapView. The code that starts the animation does not have to be in the view itself; A better place to code might be a view controller. Think of using NSNotificationCenterto notify the view controller that the annotation is requesting fade-out-and-remove.

0
source

Source: https://habr.com/ru/post/1531018/


All Articles