iOS 11.x swift 4.0
了解 mapview 并使用此代码创建了一个带有左右 accessoryview 的图钉。
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation {
return nil
}
let reuseId = "pin"
var pav:MKPinAnnotationView?
if (pav == nil)
{
pav = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pav?.isDraggable = true
pav?.canShowCallout = true;
pav?.rightCalloutAccessoryView = UIButton(type: .infoLight)
pav?.leftCalloutAccessoryView = UIButton(type: .contactAdd)
}
else
{
pav?.annotation = annotation;
}
return pav;
}
使用此调用来检测您何时按下 infoLight 和/或 contactAdd UIButtons。但我很难弄清楚如何分辨按下了哪个?这个电话火了?但是怎么判断是左还是右呢?
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
print("go figure annotationView \(view)")
if (view.rightCalloutAccessoryView != nil) {
print("right \(view.rightCalloutAccessoryView)")
}
}
显然这是错误的,但是如何知道点击的是左侧还是右侧?
最佳答案
试试下面的代码:
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
if view.rightCalloutAccessoryView == control {
//right accessory
} else {
// left Accessory
}
}
关于ios - MKMapView 检测左或右,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49473977/