objective-c - 相当于 "didAddAnnotationViews"用于移除引脚?

标签 objective-c mkmapview mkannotation

我有一个 map View 。我已经实现了 didAddAnnotationViews 来为我的图钉显示自定义淡入淡出动画。

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

最佳答案

我已经使用以下方法为 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;
        }
    }
}

关于objective-c - 相当于 "didAddAnnotationViews"用于移除引脚?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6726621/

相关文章:

iOS:访问4.0以下的OlderBluetoothDevice

iphone - 我怎样才能知道MKMapview何时setRegion :animated: has finished?

ios - 在 MKMapView 中渲染 map 线我的 map 被卡住

ios - UIImageView 作为 callout 的 leftCalloutAccessoryView 自动移动左上角

ios - 如何打开包含UIview或图像的弹出窗口

ios - 在 Swift 框架中添加 Objective C 框架

iphone - 如何始终显示 map View 标注?

ios - MKAnnotation 的唯一性

ios - 调试运行到具有保留周期的 block

ios - 带有集群 : how to display multiple annotations on same view 的 MapView