我创建了一张 map ,上面布满了很多图钉,点击图钉会弹出默认的“气泡”,现在没问题。
我真正想做的是完全不弹出气泡,而是调用一个不同的函数。我的所有搜索结果都导致人们想要创建具有不同 View 的新自定义注释等,我只想调用一个函数,但我不确定我会尝试从哪里调用它。我是 ios 开发的新手,这看起来应该非常简单,但我发现通常情况并非如此。
最佳答案
首先,值得注意的是,标准 UX 是显示一个标注气泡,向用户显示足够的信息以确认这是预期的注释 View ,然后标注包括左和/或右附件 View (例如按钮标注的左侧和/或右侧),这样用户就可以使用该注释执行一些额外的任务。参见 Creating Callouts在位置和 map 编程指南中。
但是,如果您想在用户点击注释时立即执行其他操作,请设置您的注释 View ,以便 (a) 它没有标注;然后 (b) 实现 MKMapViewDelegate
方法 didSelectAnnotationView
当用户点击注释 View 时执行您想要的任何任务。
例如,假设您已经为 map View 指定了 delegate
,您可以在 Swift 3/4 中执行类似以下操作:
private let reuseIdentifier = "MyIdentifier"
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation { return nil }
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseIdentifier) as? MKPinAnnotationView
if annotationView == nil {
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)
annotationView?.tintColor = .green // do whatever customization you want
annotationView?.canShowCallout = false // but turn off callout
} else {
annotationView?.annotation = annotation
}
return annotationView
}
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
// do something
}
或者在 Swift 2 中:
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation { return nil }
var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseIdentifier) as? MKPinAnnotationView
if annotationView == nil {
annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)
annotationView?.tintColor = UIColor.greenColor() // do whatever customization you want
annotationView?.canShowCallout = false // but turn off callout
} else {
annotationView?.annotation = annotation
}
return annotationView
}
func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
// do something
}
关于ios - 当我点击 MapKit 中的图钉时如何执行操作? ios9, swift 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37247220/