我有一个 map View 。我已经实现了 didAddAnnotationViews 来为我的图钉显示自定义淡入淡出动画。
当大头针被添加到 map 时调用成功,但当大头针被移除时则不会。我在文档中找不到等效的函数。是否有另一种方法可以为特定引脚实现自定义淡出动画?
最佳答案
我已经使用以下方法为 MKMapView 创建了类别
- (void)removeAnnotation:(id<MKAnnotation>)annotation animated:(BOOL)shouldAnimate;
- (void)removeAnnotations:(NSArray *)annotations animated:(BOOL)shouldAnimate;
你可以调用而不是调用
- (void)removeAnnotation:(id<MKAnnotation>)annotation;
- (void)removeAnnotations:(NSArray *)annotations;
实现如下:
- (void)removeAnnotation:(id<MKAnnotation>)annotation animated:(BOOL)shouldAnimate {
if (!shouldAnimate)
[self removeAnnotation:annotation];
else {
MKAnnotationView *annotationView = [self viewForAnnotation:annotation];
CGRect endFrame = annotationView.frame;
endFrame = CGRectMake(
annotationView.frame.origin.x,
annotationView.frame.origin.y - self.bounds.size.height,
annotationView.frame.size.width,
annotationView.frame.size.height);
[UIView animateWithDuration:0.3
delay:0.0f
options:UIViewAnimationOptionAllowUserInteraction
animations:^{
annotationView.frame = endFrame;
}
completion:^(BOOL finished) {
[self removeAnnotation:annotation];
}];
}
}
- (void)removeAnnotations:(NSArray *)annotations animated:(BOOL)shouldAnimate {
if (!shouldAnimate)
[self removeAnnotations:annotations];
else {
NSTimeInterval delay = 0.0;
for (id<MKAnnotation> annotation in annotations) {
MKAnnotationView *annotationView = [self viewForAnnotation:annotation];
CGRect endFrame = annotationView.frame;
endFrame = CGRectMake(
annotationView.frame.origin.x,
annotationView.frame.origin.y - self.bounds.size.height,
annotationView.frame.size.width,
annotationView.frame.size.height);
[UIView animateWithDuration:0.3
delay:delay
options:UIViewAnimationOptionAllowUserInteraction
animations:^{
annotationView.frame = endFrame;
}
completion:^(BOOL finished) {
[self removeAnnotation:annotation];
}];
delay += 0.05;
}
}
}
关于objective-c - 相当于 "didAddAnnotationViews"用于移除引脚?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6726621/