简体   繁体   中英

UITapGestureRecognizer on MKMapView breaks MKAnnotation selection

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 .

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);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM