我有一个 MKMapView,我向它添加了一些注释。我想在添加一段时间后一一删除注释。我需要每个注释都有自己的生命周期。是否可以?我应该如何做到这一点?
2 回答
您最好的选择是使用-(void)removeAnnotations:(NSArray *)annotations
您的MKMapView
.
只需将您的注释保存在某处,例如带有 {date : annotationObject} 的 NSDictionary,并在您想要删除时检索它。
例如:
//Call somewhere to delete after 2 seconds
[self performSelector:@selector(deleteAnnotation:) withObject:annotation afterDelay:2.f]
//this function will remove the annotation from your map
-(void) deleteAnnotation:(id) object{
[self.map removeAnnotations:@[object]];
}
MKMapView 具有删除已添加注释的方法removeAnnotation
。removeAnnotations
如果您希望每个注释都有自己的生命周期:
AnnotationLifespanDelegate
使用以注释作为参数(例如func dearAnnotationRIP(annotation : MKAnnotation)
)的指示生命周期结束的方法创建协议。lifespan
使用andlifeSpanDelegate
属性和方法创建自定义 MKAnnotationstartCountdown
。startCountdown
方法只是启动一个间隔等于 的计时器,lifespan
并在委托上调用生命周期结束方法。在视图控制器中实现
AnnotationLifespanDelegate
方法并在创建注解对象时,确保在将注解添加到地图视图后立即设置和调用注解上lifespan
的delegate
方法。startCountdown
在注解生命周期结束方法中,从地图中移除注解。