Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove an annotation from a map view gently?

Tags:

ios

mkmapview

I'm adding and removing annotations to a map view. When I remove one it disappears abruptly and looks a bit startling, I'd rather it faded away gracefully.

I tried removing it with UIView:animateWithDuration: but it wasn't an animatable attribute.

If there's no other easy solution I was thinking I could get the annotation view to fade its alpha and then remove its annotation from the map. However the problem with this is it doesn't seem like an annotation view has a reference to its map view? Adding one starts to get a bit messy. Is there some easy quick solution to removing an annotation gracefully?

like image 350
Gruntcakes Avatar asked Mar 10 '14 17:03

Gruntcakes


1 Answers

Using animateWithDuration should work fine. To fade the removal of an annotation, one 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];
}
like image 94
Rob Avatar answered Sep 27 '22 19:09

Rob