Reputation: 3357
In my mapview I am using the long press gesture to draw an MKCircle on the map. I'd like to use a double tap gesture to remove the circle. I add double tap as a gesture recognizer and it works correctly, however, while it removes the circle it also zooms a bit each time. I am wondering if there is a way to remove the default zoom by double tap behavior leaving just my own? I don't want to disable zooming for the whole map just when doing a double tap.
UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(getMapCoordinateFromTouch:)];
[self.mapView addGestureRecognizer:longGesture ];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(removeBoundary:)];
[tapGesture setNumberOfTapsRequired:2];
[tapGesture setNumberOfTouchesRequired:1];
tapGesture.delegate = self;
[self.mapView addGestureRecognizer:tapGesture];
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
-(void)removeBoundary:(UITapGestureRecognizer *)gesture
{
[self.mapView removeOverlays:self.mapView.overlays];
}
Additional Info:
It was suggested that this question was similar to: Disable double tap zoom in MKMapView (iOS 6)
However, that person was trying to disable all double taps and not just the default behavior. I did find some code in that question that I thought may help here but it does not. In that thread it was indicated that you could loop through the mapview and remove the gesture recognizer. This seems to work for gesture recognizers that I may add but it does not find any of the Apple default behaviors. I run the following code after viewWillAppear (I also tried viewDidAppear) but a break point shows that "gestures" is nil. So for some reason the view does not have Apple's default gestures.
NSArray *gestures = [self.mapView gestureRecognizers];
for (UIGestureRecognizer *recognizer in gestures)
{
if ([recognizer isKindOfClass:[UITapGestureRecognizer class]])
{
UITapGestureRecognizer *tap = (UITapGestureRecognizer *)recognizer;
if (tap.numberOfTapsRequired == 2)
[self.mapView removeGestureRecognizer:recognizer];
}
}
Upvotes: 1
Views: 1836
Reputation: 820
Default UIGestureRecognizers appear to be added to the first subview, just remove them:
Example to remove all UITapGestureRecognizers (go from 13 to 9)
Swift:
print("GestureRecognizers before \(mainMap.subviews[0].gestureRecognizers?.count)")
if (mainMap.subviews[0].gestureRecognizers != nil){
for gesture in mainMap.subviews[0].gestureRecognizers!{
if (gesture.isKindOfClass(UITapGestureRecognizer)){
mainMap.subviews[0].removeGestureRecognizer(gesture)
}
}
}
print("GestureRecognizers after \(mainMap.subviews[0].gestureRecognizers?.count)")
Objective-C:
NSArray *gestures = [self.mapView.subviews.firstObject gestureRecognizers];
for (UIGestureRecognizer *recognizer in gestures)
{
if ([recognizer isKindOfClass:[UITapGestureRecognizer class]])
{
UITapGestureRecognizer *tap = (UITapGestureRecognizer *)recognizer;
if (tap.numberOfTapsRequired == 2)
[self.mapView.subviews.firstObject removeGestureRecognizer:recognizer];
}
}
Upvotes: 4
Reputation: 124997
Try returning NO
from -gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:
. It sounds like both your recognizer and the map view's recognizer are acting on the double tap. You really want yours to get first crack at the gesture, so it can effectively override the map view's.
Upvotes: 0