簡體   English   中英

當我點擊 MapKit 中的圖釘時如何執行操作? ios9,快速 2

[英]How can I perform an action when I tap on a pin from MapKit? ios9, swift 2

我已經創建了一張地圖,上面有大量的圖釘,點擊圖釘會彈出默認的“氣泡”,現在很好。

我真正想做的是根本不彈出氣泡,而是調用不同的函數。 我所有的搜索都導致人們想要創建具有不同視圖的新自定義注釋等,我只想調用一個函數,但我不確定我會嘗試從哪里調用它。 我對 ios 開發很陌生,這看起來應該很簡單,但我發現通常情況並非如此。

首先,值得注意的是,標准 UX 是呈現一個標注氣泡,向用戶顯示足夠的信息以確認這是預期的注釋視圖,然后標注包括左和/或右附件視圖(例如左側和/或標注的右側),讓用戶可以使用該注釋執行一些額外的任務。 請參閱位置和地圖編程指南中的創建標注

金門大橋

但是,如果您想在用戶點擊注釋時立即執行其他操作,請設置您的注釋視圖,以便 (a) 它沒有標注; 然后 (b) 實現MKMapViewDelegate方法didSelectAnnotationView以在用戶點擊注釋視圖時執行您想要的任何任務。


例如,假設您已經為地圖視圖指定了delegate ,您可以在 Swift 3/4 中執行以下操作:

private let reuseIdentifier = "MyIdentifier"

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation is MKUserLocation { return nil }

    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseIdentifier) as? MKPinAnnotationView
    if annotationView == nil {
        annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)
        annotationView?.tintColor = .green                // do whatever customization you want
        annotationView?.canShowCallout = false            // but turn off callout
    } else {
        annotationView?.annotation = annotation
    }

    return annotationView
}

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    // do something
}

或者在 Swift 2 中:

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation is MKUserLocation { return nil }

    var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseIdentifier) as? MKPinAnnotationView
    if annotationView == nil {
        annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)
        annotationView?.tintColor = UIColor.greenColor()  // do whatever customization you want
        annotationView?.canShowCallout = false            // but turn off callout
    } else {
        annotationView?.annotation = annotation
    }

    return annotationView
}

func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
    // do something
}

您可以通過在MKAnnotationView上設置canShowCallout屬性來阻止標注氣泡顯示。

anView!.canShowCallout = false

第二個實現了 MapView 的didSelectAnnotationView委托方法來處理引腳選擇上的某些內容。您可以從didSelectAnnotationView調用您的方法。

func mapView(mapView: MKMapView!, didSelectAnnotationView view: MKAnnotationView!)
{
    //Pin clicked, do your stuff here
}

斯威夫特 5

override func viewDidLoad() {
   super.viewDidLoad()
   mapView.delegate = self
}

extension MapViewController: MKMapViewDelegate {

   func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
      print("calloutAccessoryControlTapped")
   }

   func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView){
      print("didSelectAnnotationTapped")
   }
}

暫無
暫無

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

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