动画删除注释

ben*_*wad 13 objective-c mkmapview mkannotation mkannotationview ios

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

Vla*_*mir 16

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

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

  • @benwad,您可以使用MKMapView中的viewForAnnotation方法获取它,虽然不能保证始终返回实际视图(例如,如果注释不可见,它可以返回nil) (2认同)

nal*_*exn 5

你应该推迟的呼叫removeAnnotation因为的MKMapView的状态可以在动画制作过程中变了样在@弗拉基米尔的答案.

到了时间removeAnnotation从动画完成块调用,另一个注解可以添加/删除从MapView的-所以,在某些情况下,你可以最终消除错误的注释集.

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

@interface MKMapView (RemoveAnnotationWithAnimation)

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

@end
Run Code Online (Sandbox Code Playgroud)

和.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
Run Code Online (Sandbox Code Playgroud)