I have a mapview. I've implemented didAddAnnotationViews to show a custom fade in animation for my pins.
This is called successfully when pins are added to the map, but not when pins are removed. I can't find an equivalent function in the documentation. Is there another way to implement a custom fade out animation for specific pins?
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;
        }
    }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With