You should NOT defer the removeAnnotation call, as in @Vladimir's answer, because the state of MKMapView can be changed during the animation.
By the time removeAnnotation is called from the animation completion block, other annotations can be added / removed to MapView, so in some cases you can delete the wrong set of annotations. p>
I wrote this category for MKMapView, which you can safely use to remove animated annotations:
@interface MKMapView (RemoveAnnotationWithAnimation) - (void)removeAnnotation:(id <MKAnnotation>)annotation animated:(BOOL)animated; - (void)removeAnnotations:(NSArray *)annotations animated:(BOOL)animated; @end
And the .m file:
@implementation MKMapView (RemoveAnnotationWithAnimation) - (void)removeAnnotation:(id <MKAnnotation>)annotation animated:(BOOL)animated { [self removeAnnotations:@[annotation] animated:animated]; } - (void)removeAnnotations:(NSArray *)annotations animated:(BOOL)animated { if (animated) { NSSet * visibleAnnotations = [self annotationsInMapRect:self.visibleMapRect]; NSMutableArray * annotationsToRemoveWithAnimation = [NSMutableArray array]; for (id<MKAnnotation> annotation in annotations) { if ([visibleAnnotations containsObject:annotation]) { [annotationsToRemoveWithAnimation addObject:annotation]; } } NSMutableArray * snapshotViews = [NSMutableArray array]; for (id<MKAnnotation> annotation in annotationsToRemoveWithAnimation) { UIView * annotationView = [self viewForAnnotation:annotation]; if (annotationView) { UIView * snapshotView = [annotationView snapshotViewAfterScreenUpdates:NO]; snapshotView.frame = annotationView.frame; [snapshotViews addObject:snapshotView]; [[annotationView superview] insertSubview:snapshotView aboveSubview:annotationView]; } } [UIView animateWithDuration:0.5 animations:^{ for (UIView * snapshotView in snapshotViews) {
NAlexN Feb 15 '15 at 10:42 2015-02-15 10:42
source share