animation去除注释

我有一张地图和一组注释,每个都有一个“父”属性。 目前,当我添加注释时,我实现了didAddAnnotationViews方法来对这些注释进行animation处理,以使它们看起来像来自其父坐标。 删除注释过程中是否有这样做的方法? 当我从地图中移除一个注解时,我想让它animation到它的父坐标中,并且据我所知,当注释被移除时,没有与addAddAnnotationViews等价的东西。

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

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

你不应该像在@ Vladimir的回答中推迟removeAnnotation的调用,因为在animation期间MKMapView的状态是可以改变的。

从animation完成块中调用removeAnnotation时 ,可以从MapView中添加/删除其他注释 – 因此,在某些情况下,您最终可能会移除错误的注释集。

我为MKMapView编写了这个类别,您可以用它来安全地移动animation注释:

 @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