我正在使用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)
你想要做什麼?使用mapView(_ mapView:MKMapView,didSelect視圖:MKAnnotationView)來處理註釋的點擊。 – kuzdu
@kuzdu我以前沒有使用過,但現在當我點擊我的自定義註釋時,標註沒有顯示。我只是嘗試使用任何方法處理自定義註釋的點擊,如果您引用的那個會起作用,請解釋如何。 – brettf