Create a UITapGestureRecognizer
and set numberOfTapsRequired
to 2
. Add this gesture recognizer to your instance of MKPinAnnotationView
. Additionally, you will need to set your controller as the delegate of the gesture recognizer and implement -gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:
and return YES
to prevent your gesture recognizer from stomping on the ones used internally by MKMapView
.
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation)annotation
{
// Reuse or create annotation view
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapRecgonized:)];
doubleTap.numberOfTapsRequired = 2;
doubleTap.delegate = self;
[annotationView addGestureRecognizer:doubleTap];
}
- (void)doubleTapRecognized:(UITapGestureRecognizer *)recognizer
{
// Handle double tap on annotation view
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gesture shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGesture
{
return YES;
}
Edit: Sorry I misunderstood. What you're describing should be possible using -mapView:didSelectAnnotationView:
and the gesture recognizer configured for only 1 required tap. The idea is that we're only going to add the gesture recognizer to an annotation view when it is selected. We'll remove it when the annotation view is deselected. This way you can handle the zooming in the -tapGestureRecognized:
method and it's guaranteed to only be executed if the annotation was already tapped.
For this I would add the gesture recognizer as a property of your class and configure it in -viewDidLoad
. Assume it is declared as @property (nonatomic, strong) UITapGestureRecognizer *tapGesture;
and that we're using ARC.
- (void)viewDidLoad
{
[super viewDidLoad];
self.tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureRecognized:)];
}
- (void)tapGestureRecognized:(UIGestureRecognizer *)gesture
{
// Zoom in even further on already selected annotation
}
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)annotationView
{
[annotationView addGestureRecognizer:self.tapGesture];
}
- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)annotationView
{
[annotationView removeGestureRecgonizer:self.tapGesture];
}