equivalent of "didAddAnnotationViews" for removing pins?
I have a mapview. I've implemented didAddAnnotationViews to show a custom fade i开发者_StackOverflown animation for my pins.
This is called successfully when pins are added to the map, but not when pins are removed. I can't find an equivalent function in the documentation. Is there another way to implement a custom fade out animation for specific pins?
I've created the category to MKMapView with the following methods
- (void)removeAnnotation:(id<MKAnnotation>)annotation animated:(BOOL)shouldAnimate;
- (void)removeAnnotations:(NSArray *)annotations animated:(BOOL)shouldAnimate;
that you can call instead of calling
- (void)removeAnnotation:(id<MKAnnotation>)annotation;
- (void)removeAnnotations:(NSArray *)annotations;
Here's the implementation:
- (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;
}
}
}
There's no delegate methods for removing annotations, but you can achieve animated effect the following way:
When you want to remove annotation, first fade its view out with animation and remove annotation when animation completes. You code may look like:
[UIView animateWithDuration:0.5f animations:^(void){
annotationView.alpha = 0.0f;
}
completion:^(BOOL finished){
[mapView removeAnnotation:annotation];
}];
精彩评论