Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: Is it possible to add a triple tap gesture recognizer to a MKMapView?

I have a MKMapView, which zooms in when double tapped (default behavior). I want to add a triple tap gesture recognizer to the map view that zooms out again to a certain initial zoom level.
The problem is that the built-in double tap recognizer fires first. In order to delay this, one had to access the double tap recognizer in some way, but at least in iOS6 the view property gestureRecognizers does not contain the double tap recognizer of the map view.
So, is it possible to delay the double tap recognizer in some way in order to allow the triple tap recognizer to fire first?

like image 447
Reinhard Männer Avatar asked May 15 '13 17:05

Reinhard Männer


1 Answers

Check out the instance method requireGestureRecognizerToFail in the UIGestureRecognizer class.

http://developer.apple.com/library/ios/documentation/uikit/reference/UIGestureRecognizer_Class/Reference/Reference.html#//apple_ref/occ/instm/UIGestureRecognizer/requireGestureRecognizerToFail:

Creates a dependency relationship between the receiver and another gesture recognizer.

For example:

[doubleTap requireGestureRecognizerToFail:tripleTap];

Update

After goofing around with it for a bit, I've got it working like this (assuming you've got an MKMapView named mapView):

UITapGestureRecognizer *tripleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTripleTap:)];
tripleTap.numberOfTapsRequired = 3;
[mapView.subviews[0] addGestureRecognizer:tripleTap];

UIView *tempMapView = mapView.subviews[0];
NSArray *mapGestures = tempMapView.gestureRecognizers;
UITapGestureRecognizer *tempMapDoubleTap = mapGestures[0];
[tempMapDoubleTap requireGestureRecognizerToFail:tripleTap];
NSLog(@"%@", mapGestures);

Triple Tap Selector:

- (void)handleTripleTap:(UIGestureRecognizer *)gestureRecognizer {
    NSLog(@"Triple Tap Detected..");
}

Now double-tapping still zooms the MKMapView and triple-tapping successfully executes handleTripleTap without zooming.

like image 88
rog Avatar answered Sep 29 '22 06:09

rog