我想在此选定的注释视图上添加单击侦听器,以打开另一个带有注释详细信息的视图控制器。我怎样才能做到这一点?
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
if let selected = (view.annotation as? CustomAnnotation) {
self.selectedAnnotation = selected
}
}
编辑
一般来说,你会添加一个rightCalloutAccessoryView
然后实现calloutAccessoryControlTapped
,如how do I make a pin annotation callout?所示
但是你说:
我需要让整个标注可以点击
MapKit没有委托方法来捕获标注上的点击,本身仅在附件视图上。但是您可以添加自己的委托来为您执行此操作。
protocol CustomAnnotationViewDelegate: class {
func didTapCallout(for annotation: MKAnnotation)
}
class CustomAnnotationView: MKPinAnnotationView {
static let preferredReuseIdentifier = Bundle.main.bundleIdentifier! + ".customAnnotationView"
weak var delegate: CustomAnnotationViewDelegate?
override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
canShowCallout = true
let tap = UITapGestureRecognizer(target: self, action: #selector(didTapAnnotationView(_:)))
self.addGestureRecognizer(tap)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
@objc func didTapAnnotationView(_ gesture: UITapGestureRecognizer) {
let location = gesture.location(in: self)
// ignore taps on the annotation view, itself
if bounds.contains(location) { return }
// if we got here, we must have tapped on the callout
delegate?.didTapCallout(for: annotation!)
}
}
然后在iOS 11及更高版本中,您可以注册此reuseIdentifier:
override func viewDidLoad() {
super.viewDidLoad()
mapView.register(CustomAnnotationView.self,
forAnnotationViewWithReuseIdentifier: CustomAnnotationView.preferredReuseIdentifier)
}
你的viewFor
可以指定代表:
extension ViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation { return nil }
let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier, for: annotation) as! CustomAnnotationView
annotationView.delegate = self
return annotationView
}
}
或者,如果您需要支持11之前的iOS版本,则不会注册重用标识符,但如果未成功出列,则必须自己手动实例化CustomAnnotationView
:
extension ViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation { return nil }
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: CustomAnnotationView.preferredReuseIdentifier) as? CustomAnnotationView
if annotationView == nil {
annotationView = CustomAnnotationView(annotation: annotation, reuseIdentifier: CustomAnnotationView.preferredReuseIdentifier)
annotationView?.delegate = self
} else {
annotationView?.annotation = annotation
}
return annotationView
}
}
无论哪种方式,您现在可以让您的视图控制器符合新的CustomAnnotationViewDelegate
:
extension ViewController: CustomAnnotationViewDelegate {
func didTapCallout(for annotation: MKAnnotation) {
print("tapped callout for \(annotation)")
}
}
但请注意,在上面,我在CustomAnnotationView
init
方法中添加了点击手势识别器,以确保在首次创建注释视图时只创建一次轻击手势。