Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animate removal of annotations

I have a map and a set of annotations, each with a 'parent' property. Currently when I add annotations I implement the didAddAnnotationViews method to animate those annotations so they appear to come from their parent's coordinate. Is there a way of doing this during the removal of annotations? When I remove an annotation from the map I want it to animate in to its parent coordinate, and as far as I know there is no equivalent for didAddAnnotationViews for when an annotation is removed.

like image 438
benwad Avatar asked Dec 19 '11 16:12

benwad


2 Answers

Animate annotation before you remove it from the map and perform removal after animation is completed. The code may look like:

- (void) removeMyAnnotation:(MyAnnotation*)annotation{
   [UIView animateWithDuration:1.0f
                    animations:^(void){
                         annotation.coordinate = annotation.parentAnnotation.coordinate;
                    }
                    completion:^(BOOL finished)completion{
                        [mapView removeAnnotation:annotation];
                    }
}
like image 175
Vladimir Avatar answered Nov 02 '22 18:11

Vladimir


You should NOT defer call of removeAnnotation like in @Vladimir's answer because state of MKMapView could be changed during the animation.

By the time the removeAnnotation is called from animation completion block, another annotations could be added / removed from the MapView - so, in some cases you can end up removing wrong set of annotations.

I wrote this category for MKMapView you can use for animated annotation removal in safe manner:

@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
like image 26
nalexn Avatar answered Nov 02 '22 16:11

nalexn