ios mapkit closing annotation callouts by tapping the map

后端 未结 2 1162
孤独总比滥情好
孤独总比滥情好 2021-01-05 14:00

I\'ve got a mapkit app that places annotations on the map, when you press them, it shows the callout with the title attribute.

This works fine, but the user cannot c

2条回答
  •  星月不相逢
    2021-01-05 14:37

    You can implement the UIGestureRecognizerDelegate method gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: and return YES (so the map view's own tap gesture recognizer will execute its method as well).

    First, add the protocol declaration to your view controller's interface (to avoid a compiler warning):

    @interface MyViewController : UIViewController 
    

    Next, set the delegate property on the gesture recognizer:

    tap.delegate = self;
    

    Finally, implement the method:

    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
        shouldRecognizeSimultaneouslyWithGestureRecognizer:
            (UIGestureRecognizer *)otherGestureRecognizer
    {
        return YES;
    }
    



    If that doesn't work out for some reason, you can alternatively de-select any currently selected annotation manually at the top of the handleTap: method:

    for (id ann in mapView.selectedAnnotations) {
        [mapView deselectAnnotation:ann animated:NO];
    }
    

    Even though the map view only allows a maximum of one annotation to be selected at a time, the selectedAnnotations property is an NSArray so we loop through it.

提交回复
热议问题