Last Updated: February 25, 2016
·
1.078K
· benliong

- (void)removeAnnotation: animated:

If you have a custom MKAnnotationView, chances are you need to implementation your own drop-pin-like animation similar to those you can do with MKPinAnnotationView

Do it in MKMapViewDelegate method

- (void) mapView: didAddAnnotationViews:

Implementation as follows:

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
    CGRect bounds = self.stationsMapView.bounds;
    NSTimeInterval delay = 0.0;
    for(MKAnnotationView *view in views) {
        CGRect endFrame = view.frame;
        view.frame = CGRectMake(view.frame.origin.x, view.frame.origin.y - bounds.size.height, view.frame.size.width, view.frame.size.height);
        [UIView animateWithDuration:0.3 
                                              delay:delay
 options:UIViewAnimationOptionAllowUserInteraction
                                      animations:^{
                                            view.frame = endFrame;
                                      } completion:nil];
        delay += 0.05;
    }
}

To do the same (reverse
I've created the category to MKMapView with the following methods

- (void)removeAnnotation:(id<MKAnnotation>)annotation animated:(BOOL)shouldAnimate;
- (void)removeAnnotations:(NSArray *)annotations animated:(BOOL)shouldAnimate;

that you can call instead of calling

- (void)removeAnnotation:(id<MKAnnotation>)annotation;
- (void)removeAnnotations:(NSArray *)annotations;

Here's the implementation:

- (void)removeAnnotation:(id<MKAnnotation>)annotation animated:(BOOL)shouldAnimate {
    if (!shouldAnimate)
        [self removeAnnotation:annotation];
    else {
        MKAnnotationView *annotationView = [self viewForAnnotation:annotation];
        CGRect endFrame = annotationView.frame;
    endFrame = CGRectMake(
                       annotationView.frame.origin.x, 
                       annotationView.frame.origin.y - self.bounds.size.height, 
                       annotationView.frame.size.width, 
                       annotationView.frame.size.height);
    [UIView animateWithDuration:0.3 
                          delay:0.0f 
                        options:UIViewAnimationOptionAllowUserInteraction
                     animations:^{
                         annotationView.frame = endFrame;
                     } 
                     completion:^(BOOL finished) {
                         [self removeAnnotation:annotation];
                     }];
    }
}

- (void)removeAnnotations:(NSArray *)annotations animated:(BOOL)shouldAnimate {
    if (!shouldAnimate)
        [self removeAnnotations:annotations];
    else {
        NSTimeInterval delay = 0.0;
        for (id<MKAnnotation> annotation in annotations) {
            MKAnnotationView *annotationView = [self viewForAnnotation:annotation];
            CGRect endFrame = annotationView.frame;
            endFrame = CGRectMake(
                              annotationView.frame.origin.x, 
                              annotationView.frame.origin.y - self.bounds.size.height, 
                              annotationView.frame.size.width, 
                              annotationView.frame.size.height);
            [UIView animateWithDuration:0.3 
                                  delay:delay
                                options:UIViewAnimationOptionAllowUserInteraction
                             animations:^{
                                 annotationView.frame = endFrame;
                             } 
                             completion:^(BOOL finished) {
                                 [self removeAnnotation:annotation];
                             }];
            delay += 0.05;
        }
    }
}