Annotation Removal Animation

I have a map and a set of annotations, each of which has a "parent" property. Currently, when I add annotations, I am implementing the didAddAnnotationViews method to animate these annotations so that they seem to come from their parent coordinate. Is there any way to do this while deleting annotations? When I delete an annotation from a map, I want it to animate at its parent coordinate, and as far as I know, there was no equivalent for didAddAnnotationViews for when the annotation is deleted.

+12
ios objective-c mkmapview mkannotationview mkannotation
Dec 19 '11 at 16:05
source share
2 answers

Animating the annotation until you delete it from the map and delete it after the animation finishes. The code might look like this:

- (void) removeMyAnnotation:(MyAnnotation*)annotation{ [UIView animateWithDuration:1.0f animations:^(void){ annotation.coordinate = annotation.parentAnnotation.coordinate; } completion:^(BOOL finished)completion{ [mapView removeAnnotation:annotation]; } } 
+15
Dec 19 '11 at 16:12
source share

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) { // Change the way views are animated if you want CGRect frame = snapshotView.frame; frame.origin.y = -frame.size.height; snapshotView.frame = frame; } } completion:^(BOOL finished) { for (UIView * snapshotView in snapshotViews) { [snapshotView removeFromSuperview]; } }]; } [self removeAnnotations:annotations]; } @end 
+5
Feb 15 '15 at 10:42
source share



All Articles