3

I've added a UITapGestureRecognizer to an MKMapView, like so:

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]
                                      initWithTarget:self 
                                      action:@selector(doStuff:)];
[tapGesture setCancelsTouchesInView:NO]; 
[tapGesture setDelaysTouchesEnded:NO]; 
[[self myMap] addGestureRecognizer:tapGesture];
[tapGesture release];

This almost works: tap gestures are recognized and double taps still zoom the map. Unfortunately, the UITapGestureRecognizer interferes with the selection and deselection of MKAnnotationView elements, which are also triggered by tap gestures.

Setting the setCancelsTouchesInView and setDelaysTouchesEnded properties doesn't have any effect. Annotation selection works fine if I don't add the UIGestureRecognizer.

What am I missing?

UPDATE:

As suggested by Anna Karenina below, this problem can be avoided by returning YES in the shouldRecognizeSimultaneouslyWithGestureRecognizer: delegate method.

More details in this answer.

Community
  • 1
  • 1
carton
  • 1,020
  • 1
  • 8
  • 17
  • 1
    You will most likely need to subclass MKMapView and overwrite `touchesBegan:` etc... instead of using gesture recognisers. From there you can identify whether the touch was originated from the annotation and pass it to the super view, or capture it depending on your implementation. – Rog Jan 09 '12 at 03:30
  • 3
    Try [this answer](http://stackoverflow.com/a/6455734/467105) which suggests implementing shouldRecognizeSimultaneouslyWithGestureRecognizer. –  Jan 09 '12 at 03:47
  • Thanks for the replies! @AnnaKarenina's answer did the trick. It's not necessary to subclass MKMapView. – carton Jan 09 '12 at 20:39

1 Answers1

0

Instead of tap gesture, add long press gesture as below :-

UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] 
        initWithTarget:self action:@selector(longpressToGetLocation:)];
    lpgr.minimumPressDuration = 2.0;  //user must press for 2 seconds
    [mapView addGestureRecognizer:lpgr];
    [lpgr release];


- (void)longpressToGetLocation:(UIGestureRecognizer *)gestureRecognizer
{
    if (gestureRecognizer.state != UIGestureRecognizerStateBegan)
        return;

    CGPoint touchPoint = [gestureRecognizer locationInView:self.mapView];   
    CLLocationCoordinate2D location = 
        [self.mapView convertPoint:touchPoint toCoordinateFromView:self.mapView];

    NSLog(@"Location found from Map: %f %f",location.latitude,location.longitude);

}
Bosoud
  • 158
  • 4
  • 24
  • I got it to work with the tap gesture by implementing `shouldRecognizeSimultaneouslyWithGestureRecognizer:` (see above) – carton Feb 12 '13 at 16:19