MKMapView 添加注释并在一段时间后将其删除
MKMapView add annotation and remove it after some time
我有一个 MKMapView 并向其添加了一些注释。我想在添加注释一段时间后将其一一删除。我需要每个注释都有自己的生命周期。可能吗?我该如何实现?
您最好的选择是使用 MKMapView
中的 -(void)removeAnnotations:(NSArray *)annotations
。
只需将您的注释保存在某处,例如带有 {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
和 lifeSpanDelegate
属性以及 startCountdown
方法创建自定义 MKAnnotation。
startCountdown
方法简单地启动一个定时器,其间隔等于 lifespan
并在委托上调用生命周期结束方法。
在视图控制器中实现 AnnotationLifespanDelegate
方法,并在创建注释对象时确保设置 lifespan
和 delegate
并调用 startCountdown
方法添加到地图视图后立即在注释上。
在注释生命周期结束方法中,从地图中删除注释。
我有一个 MKMapView 并向其添加了一些注释。我想在添加注释一段时间后将其一一删除。我需要每个注释都有自己的生命周期。可能吗?我该如何实现?
您最好的选择是使用 MKMapView
中的 -(void)removeAnnotations:(NSArray *)annotations
。
只需将您的注释保存在某处,例如带有 {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
和lifeSpanDelegate
属性以及startCountdown
方法创建自定义 MKAnnotation。startCountdown
方法简单地启动一个定时器,其间隔等于lifespan
并在委托上调用生命周期结束方法。在视图控制器中实现
AnnotationLifespanDelegate
方法,并在创建注释对象时确保设置lifespan
和delegate
并调用startCountdown
方法添加到地图视图后立即在注释上。在注释生命周期结束方法中,从地图中删除注释。