相当于“didAddAnnotationViews”用于删除引脚?

时间:2011-07-17 20:37:49

标签: objective-c mkmapview mkannotation

我有一个mapview。我已经实现了didAddAnnotationViews来为我的引脚显示动画的自定义淡入淡出。

将引脚添加到地图时会成功调用此引脚,但在引脚被移除时则不会。我在文档中找不到等效的功能。是否有另一种方法可以为特定引脚实现自定义淡出动画?

2 个答案:

答案 0 :(得分:5)

我使用以下方法为MKMapView创建了类别

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

你可以打电话而不是打电话

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

以下是实施:

- (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;
        }
    }
}

答案 1 :(得分:3)

没有用于删除注释的委托方法,但您可以通过以下方式实现动画效果:

如果要删除注释,首先使用动画淡出其视图,并在动画完成时删除注释。您的代码可能如下所示:

[UIView animateWithDuration:0.5f animations:^(void){
                            annotationView.alpha = 0.0f;
                        }
                 completion:^(BOOL finished){
                     [mapView removeAnnotation:annotation];
                 }];