Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

equivalent of "didAddAnnotationViews" for removing pins?

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?

like image 788
thekevinscott Avatar asked Jul 17 '11 20:07

thekevinscott


1 Answers

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;
        }
    }
}
like image 142
Ben Avatar answered Oct 04 '22 15:10

Ben