2017-05-20 27 views
0

我正在使用MKPointAnnotation個斯威夫特應用程序,我最近碰到了,我需要存儲元數據在我上的註釋的問題,所以我創建下面的自定義類:定製MKPointAnnotation不響應用戶交互

class BRETTFAnnotation: MKPointAnnotation { 

    var tag: Int64 

    var name: String 

    init(lat : Double, lon:Double, t : Int64, n: String) { 

     self.tag = t 
     self.name = n 

     super.init() 


     self.coordinate = CLLocationCoordinate2D(latitude: lat, longitude: lon) 
    } 

} 

MKAnnotationView viewfor MKAnnotation方法如下所示:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 
    let newAnnotation = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "reuse") 

    newAnnotation.canShowCallout = true 

    let right = self.button(title: "Yes") 
    right?.addTarget(self, action: #selector(clickedToConfirmNewPoint), for: .touchUpInside) 
    newAnnotation.rightCalloutAccessoryView = right 

    let left = self.button(title: "No") 
    left?.addTarget(self, action: #selector(clickedToCancelNewPoint), for: .touchUpInside) 
    newAnnotation.leftCalloutAccessoryView = left 

    return newAnnotation 
} 

我遇到的問題是,當我過我的自定義BRETTFAnnotation單擊(我添加到我的MKMapView)沒有任何反應。當我點擊地圖時MKPointAnnotation(而不是BRETTFAnnotation)時,MKAnnotationView上的兩個按鈕會顯示。 我試圖讓MKPinAnnotationView使用我的BRETTFAnnotation而不是MKPointAnnotation來觸摸顯示。

如何在用戶同時單擊註釋時繼續使用自定義註釋並顯示標註?

編輯1:因爲它可能是有用的下面的代碼是我如何使註解並將其添加到mapView。

 let location = gestureRecognizer.location(in: mapView) 
     let coordinate = mapView.convert(location,toCoordinateFrom: mapView) 

     print("adding lat,long \(coordinate.latitude),\(coordinate.longitude)") 


     lastPoint = BRETTFAnnotation(lat: coordinate.latitude, lon: coordinate.longitude, t: 1, n: "") 

     let annotationView = MKPinAnnotationView(annotation: lastPoint, reuseIdentifier: "reuse") 

     mapView.addAnnotation(lastPoint) 
+0

你想要做什麼?使用mapView(_ mapView:MKMapView,didSelect視圖:MKAnnotationView)來處理註釋的點擊。 – kuzdu

+0

@kuzdu我以前沒有使用過,但現在當我點擊我的自定義註釋時,標註沒有顯示。我只是嘗試使用任何方法處理自定義註釋的點擊,如果您引用的那個會起作用,請解釋如何。 – brettf

回答

0

我通過使我BRETTFAnnotation的子類解決這個問題NSObjectMKAnnotation而不是MKPointAnnotation。這樣做可以讓我的自定義班級接收用戶交互並顯示標註。

0

當您使用自己的MKAnnoation時,您可以在didSelect中處理您的操作。只需實現以下代碼。

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) { 
    if let yourAnnotation = view.annotation as? BRETTFAnnotation { 
     //handle your meta data or/and show UIViews or whatever 
    } 
} 

與 FUNC MapView類(_ MapView類:的MKMapView,didDeselect觀點:MKAnnotationView){當你在地圖上或在其他註釋(而不是選擇的註釋之前) //隱藏UIViews或點擊 //獲取調用做你想做的 }

但這對我來說工作:

class ViewController: UIViewController, MKMapViewDelegate { 

    func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) { 


    print("didSelect") 
    if let annoation = view.annotation as? MyAnnoation { 
     print("metatag \(annoation.metaTag)") 
    } 
} 

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





override func viewDidLoad() { 
    super.viewDidLoad() 


    mapView.delegate = self 
    let annotation = MyAnnoation(n: "name", m: "metaTag") 
    annotation.coordinate = CLLocationCoordinate2D(latitude: 50.0, longitude: 8.0) 
    mapView.addAnnotation(annotation) 
    } 
} 



class MyAnnoation: MKPointAnnotation { 


var name: String? 
var metaTag: String? 


init(n: String, m: String) { 
    self.name = n 
    self.metaTag = m 
    } 
} 
+0

您的第一個方法似乎不會響應我在我的註釋上的點擊(我添加了打印語句,點擊時沒有打印)。這是否意味着我只是在其他地方做錯了什麼,或者它還與我的自定義類有關? – brettf

+0

另外我的自定義'BRETTFAnnotation'是'MKPointAnnotation'的子類,不是'MKAnnotationView'(我不知道這是否有所作爲)。 – brettf

+0

您是否設置了mapview.delegate = self?是didSelect沒有響應或內部的聲明? – kuzdu