我在我的MKAnnotation
上添加了一个左键。我希望它能够显示这个以及我在下面设置的右键和图像。下面的代码显示了这两个按钮,但只识别右键点击我的segue NewEntry
。如何指定calloutAccessoryControl
来识别点击左键,以及点击右键,并从左边的按钮添加额外的segue到左边的细节为我的LeftButtonDetailViewController
?如何从MKannotation上的第二个按钮继续?
这里是我的代码:
//MARK: - MKMapview Delegate
extension MapViewController: MKMapViewDelegate {
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
guard let subtitle = annotation.subtitle! else { return nil }
if (annotation is SpecimenAnnotation) {
if let annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(subtitle) {
return annotationView
} else {
let currentAnnotation = annotation as! SpecimenAnnotation
let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: subtitle)
switch subtitle {
case "Uncategorized":
annotationView.image = UIImage(named: "IconUncategorized")
case "Documentaries":
annotationView.image = UIImage(named: "IconDocumentaries")
default:
annotationView.image = UIImage(named: "IconUncategorized")
}
annotationView.enabled = true
annotationView.canShowCallout = true
let detailDisclosure1 = UIButton(type: UIButtonType.DetailDisclosure)
let detailDisclosure2 = UIButton(type: UIButtonType.InfoDark)
annotationView.rightCalloutAccessoryView = detailDisclosure1
annotationView.leftCalloutAccessoryView = detailDisclosure2
if currentAnnotation.title == "Empty" {
annotationView.draggable = true
}
return annotationView
}
}
return nil
}
func mapView(mapView: MKMapView, didAddAnnotationViews views: [MKAnnotationView]) {
for annotationView in views {
if (annotationView.annotation is SpecimenAnnotation) {
annotationView.transform = CGAffineTransformMakeTranslation(0, -500)
UIView.animateWithDuration(0.5, delay: 0.0, options: .CurveLinear, animations: {
annotationView.transform = CGAffineTransformMakeTranslation(0, 0)
}, completion: nil)
}
}
}
func mapView(mapView: MKMapView, annotationView: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
if let specimenAnnotation = annotationView.annotation as? SpecimenAnnotation {
performSegueWithIdentifier("NewEntry", sender: specimenAnnotation)
}
}
func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, didChangeDragState newState: MKAnnotationViewDragState, fromOldState oldState: MKAnnotationViewDragState) {
if newState == .Ending {
view.dragState = .None
}
}
}
顺便说一句,如果'dequeue'在' viewForAnnotation'成功后,在返回'annotationView'之前,必须将视图的'annotation'设置为传递给此方法的'annotation'。您还应该执行其他必要的配置,这些配置可能会针对新的'annotation'进行更改(例如,在'title'的基础上设置'draggable')。 – Rob