定制MKPointAnnotation不响应用户交互
问题描述:
我正在使用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)
答
我通过使我BRETTFAnnotation
的子类解决这个问题NSObject
和MKAnnotation
而不是MKPointAnnotation
。这样做可以让我的自定义班级接收用户交互并显示标注。
答
当您使用自己的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
}
}
你想要做什么?使用mapView(_ mapView:MKMapView,didSelect视图:MKAnnotationView)来处理注释的点击。 – kuzdu
@kuzdu我以前没有使用过,但现在当我点击我的自定义注释时,标注没有显示。我只是尝试使用任何方法处理自定义注释的点击,如果您引用的那个会起作用,请解释如何。 – brettf