I have multiple annotations set up on an MKMapView. Instead of using map annotation callouts when a pin is clicked, I instead want to animate a subview (self.detailView
) up into the bottom of the screen, and move it back out when nothing is selected. When the user has a pin selected, and then selects another pin, I want my view to animate off screen, and then immediately animate back on screen (with different information corresponding to the newly selected annotation, of course).
Without thinking too hard, I tried what seemed like the easy thing to do - when annotation is selected, animate self.detailView
onto screen, and when it is deselected, animate self.detailView
off screen:
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
NSLog( @"selected annotation view" );
[UIView animateWithDuration:0.2f
delay:0.0f
options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction
animations:^{
[self.detailView setFrame:CGRectMake(0, 307, 320, 60)];
}
completion:^(BOOL finished){
}];
}
- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
{
NSLog( @"deselected annotation view" );
[UIView animateWithDuration:0.2f
delay:0.0f
options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction
animations:^{
[self.detailView setFrame:CGRectMake(0, 367, 320, 60)];
}
completion:^(BOOL finished){
}];
}
This works fine when no pin is selected and the user selects a pin, and when a pin is selected and the user deselects it by clicking on empty space. Of course, the problem occurs when a pin is already selected: if the user then clicks on another pin, didDeselectAnnotationView
and didSelectAnnotationView
are fired in rapid succession, both animations try to run at the same time, and as a result the effect doesn't work properly. Normally I would chain the animations together by putting the second animation in the completion block of the first, but since they are in separate methods I obviously can't do that here.
Anyone have any ideas on how I might be able to get around this issue? Thanks!