动画删除注释

时间:2011-12-19 16:05:55

标签: objective-c ios mkmapview mkannotation mkannotationview

我有一个地图和一组注释,每个注释都有一个“父”属性。目前,当我添加注释时,我实现了didAddAnnotationViews方法来为这些注释设置动画,使它们看起来像是来自父级的坐标。在删除注释期间有没有办法做到这一点?当我从地图中删除注释时,我希望它在其父坐标中设置动画,据我所知,当删除注释时,didAddAnnotationViews没有等效物。

2 个答案:

答案 0 :(得分:16)

动画注释之前将其从地图中移除并在动画完成后执行删除。代码可能如下所示:

- (void) removeMyAnnotation:(MyAnnotation*)annotation{
   [UIView animateWithDuration:1.0f
                    animations:^(void){
                         annotation.coordinate = annotation.parentAnnotation.coordinate;
                    }
                    completion:^(BOOL finished)completion{
                        [mapView removeAnnotation:annotation];
                    }
}

答案 1 :(得分:5)

你应该延迟调用 removeAnnotation ,就像在@ Vladimir的回答中一样,因为在动画期间可以更改MKMapView的状态。

当从动画完成块调用 removeAnnotation 时,可以在MapView中添加/删除另一个注释 - 因此,在某些情况下,您最终可能会删除错误的注释集。

我为MKMapView写了这个类别,你可以用安全的方式去除动画注释:

@interface MKMapView (RemoveAnnotationWithAnimation)

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

@end

.m文件:

@implementation MKMapView (RemoveAnnotationWithAnimation)

- (void)removeAnnotation:(id <MKAnnotation>)annotation animated:(BOOL)animated
{
    [self removeAnnotations:@[annotation] animated:animated];
}

- (void)removeAnnotations:(NSArray *)annotations animated:(BOOL)animated
{
    if (animated) {
        NSSet * visibleAnnotations = [self annotationsInMapRect:self.visibleMapRect];
        NSMutableArray * annotationsToRemoveWithAnimation = [NSMutableArray array];
        for (id<MKAnnotation> annotation in annotations) {
            if ([visibleAnnotations containsObject:annotation]) {
                [annotationsToRemoveWithAnimation addObject:annotation];
            }
        }
        NSMutableArray * snapshotViews = [NSMutableArray array];
        for (id<MKAnnotation> annotation in annotationsToRemoveWithAnimation) {
            UIView * annotationView = [self viewForAnnotation:annotation];
            if (annotationView) {
                UIView * snapshotView = [annotationView snapshotViewAfterScreenUpdates:NO];
                snapshotView.frame = annotationView.frame;
                [snapshotViews addObject:snapshotView];
                [[annotationView superview] insertSubview:snapshotView aboveSubview:annotationView];
            }
        }
        [UIView animateWithDuration:0.5
                         animations:^{
                             for (UIView * snapshotView in snapshotViews) {
                                 // Change the way views are animated if you want
                                 CGRect frame = snapshotView.frame;
                                 frame.origin.y = -frame.size.height;
                                 snapshotView.frame = frame;
                             }
                         }
                         completion:^(BOOL finished) {
                             for (UIView * snapshotView in snapshotViews) {
                                 [snapshotView removeFromSuperview];
                             }
                         }];
    }
    [self removeAnnotations:annotations];
}

@end