I have a map and a set of annotations, each with a 'parent' property. Currently when I add annotations I implement the didAddAnnotationViews method to animate those annotations so they appear to come from their parent's coordinate. Is there a way of doing this during the removal of annotations? When I remove an annotation from the map I want it to animate in to its parent coordinate, and as far as I know there is no equivalent for didAddAnnotationViews for when an annotation is removed.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Animate annotation before you remove it from the map and perform removal after animation is completed. The code may look like:
- (void) removeMyAnnotation:(MyAnnotation*)annotation{
[UIView animateWithDuration:1.0f
animations:^(void){
annotation.coordinate = annotation.parentAnnotation.coordinate;
}
completion:^(BOOL finished)completion{
[mapView removeAnnotation:annotation];
}
}
回答2:
You should NOT defer call of removeAnnotation like in @Vladimir's answer because state of MKMapView could be changed during the animation.
By the time the removeAnnotation is called from animation completion block, another annotations could be added / removed from the MapView - so, in some cases you can end up removing wrong set of annotations.
I wrote this category for MKMapView you can use for animated annotation removal in safe manner:
@interface MKMapView (RemoveAnnotationWithAnimation)
- (void)removeAnnotation:(id <MKAnnotation>)annotation animated:(BOOL)animated;
- (void)removeAnnotations:(NSArray *)annotations animated:(BOOL)animated;
@end
And the .m file:
@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