7

I am creating a simple point annotation with a callout inside the UITapGestureRecognizer delegate.

The first time I tap on the map, the pin appears with the callout but the callout immediately disappears after that.

The second time I tap on the same pin, the callout appears and stays there, not sure why it disappears at the first time.

@IBAction func handleMapTouch(recognizer: UITapGestureRecognizer){
    let view = recognizer.view
    let touchPoint=recognizer.locationInView(view)
    var touchCord=CLLocationCoordinate2D()

    touchCord = mapView.convertPoint(touchPoint, toCoordinateFromView:
     mapView)

        mapView.removeAnnotations(mapView.annotations)
        pointAnnotation.coordinate=touchCord
        pointAnnotation.title="ABC"
        pointAnnotation.subtitle="DEF"

        mapView.addAnnotation(pointAnnotation)
        mapView.selectAnnotation(pointAnnotation, animated: true)


}
4

2 回答 2

2

以防万一其他人有同样的问题,虽然基思的回答有效,但在我的情况下,它会破坏与地图相关的其他手势,如捏合和缩放。

对我来说,将显示标注的动作延迟几毫秒效果更好。

在斯威夫特 3 中:

let deadlineTime = DispatchTime.now() + .milliseconds(500)
DispatchQueue.main.asyncAfter(deadline: deadlineTime) {
  mapView.addAnnotation(pointAnnotation)
  mapView.selectAnnotation(pointAnnotation, animated: true)
}
于 2017-01-12T14:49:39.010 回答
1

我也有同样的问题。我也不知道如何解决它,但我找到了解决方法。也许它也可以帮助你。

我曾经LongPressGesture更换TapGesture

在 Viewdidload 中:

let longPress = UILongPressGestureRecognizer(target: self, action: "addAnnotation:")
longPress.minimumPressDuration = 0.1
self.mapView.addGestureRecognizer(longPress)

在函数 addAnnotation 中:

if(gestureRecognizer.state == .Ended){
    self.mapView.removeGestureRecognizer(gestureRecognizer)

    //remove all annotation on the map
    self.mapView.removeAnnotations(self.mapView.annotations)

    //convert point user tapped to coorinate
    let touchPoint: CGPoint! = gestureRecognizer.locationInView(self.mapView)
    let touchMapCoordinate: CLLocationCoordinate2D = self.mapView.convertPoint(touchPoint, toCoordinateFromView: self.mapView)
    showCustomAnnotation(touchMapCoordinate)
}
self.mapView.addGestureRecognizer(gestureRecognizer)
于 2016-03-11T06:26:40.303 回答