简体   繁体   English

如何轻轻地从地图视图中删除注释?

[英]How to remove an annotation from a map view gently?

I'm adding and removing annotations to a map view. 我正在添加和删除注释到地图视图。 When I remove one it disappears abruptly and looks a bit startling, I'd rather it faded away gracefully. 当我删除一个它突然消失,看起来有点惊人,我宁愿它优雅地消失。

I tried removing it with UIView:animateWithDuration: but it wasn't an animatable attribute. 我尝试用UIView:animateWithDuration删除它:但它不是一个可动画的属性。

If there's no other easy solution I was thinking I could get the annotation view to fade its alpha and then remove its annotation from the map. 如果没有其他简单的解决方案,我认为我可以获取注释视图以淡化其alpha,然后从地图中删除其注释。 However the problem with this is it doesn't seem like an annotation view has a reference to its map view? 但问题是它看起来似乎没有注释视图对其地图视图的引用? Adding one starts to get a bit messy. 添加一个开始变得有点凌乱。 Is there some easy quick solution to removing an annotation gracefully? 是否有一些简单快速的解决方案来优雅地删除注释?

Using animateWithDuration should work fine. 使用animateWithDuration应该可以正常工作。 To fade the removal of an annotation, one can: 要淡化删除注释,可以:

MKAnnotationView *view = [self.mapView viewForAnnotation:annotation];

if (view) {
    [UIView animateWithDuration:0.5 delay:0.0 options:0 animations:^{
        view.alpha = 0.0;
    } completion:^(BOOL finished) {
        [self.mapView removeAnnotation:annotation];
        view.alpha = 1.0;   // remember to set alpha back to 1.0 because annotation view can be reused later
    }];
} else {
    [self.mapView removeAnnotation:annotation];
}

I think your proposed solution is the correct one. 我认为您提出的解决方案是正确的。 Set up an animation to opacity = 0, then, upon completion, remove the annotation from the MKMapView . 将动画设置为opacity = 0,然后在完成后从MKMapView删除注释。 The code that starts the animation doesn't have to be in the view itself; 启动动画的代码不必在视图中; a better location for the code may be the view controller. 代码的更好位置可以是视图控制器。 Consider using NSNotificationCenter to notify the view controller that an annotation is requesting fade-out-and-remove. 考虑使用NSNotificationCenter通知视图控制器注释正在请求淡出和删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM