簡體   English   中英

出現MKMapView上的PointAnnotation標注,然后立即消失

[英]PointAnnotation callout on MKMapView appears and then immidiately disappears

我正在UITapGestureRecognizer委托中使用callout創建一個簡單的點注釋。

我第一次點擊地圖時,引腳會出現帶有標注,但標注會立即消失。

第二次點擊相同的引腳時,標注出現並停留在那里,不知道為什么它會在第一次消失。

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


}

為了防止其他人遇到同樣的問題,雖然Keith的答案有效,但就我而言,它會破壞與地圖相關的其他手勢,例如捏合和縮放。

對我來說,延遲幾毫秒顯示標注的動作效果更好。

在Swift 3中:

let deadlineTime = DispatchTime.now() + .milliseconds(500)
DispatchQueue.main.asyncAfter(deadline: deadlineTime) {
  mapView.addAnnotation(pointAnnotation)
  mapView.selectAnnotation(pointAnnotation, animated: true)
}

我也有同樣的問題。 我也不知道如何解決它,但我找到了解決方法。 也許它也可以幫助你。

我使用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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM