首先应该注意的是,对标注的最简单更改是通过简单地调整系统提供的标注的属性来启用的,但是自定义左右配件(通过rightCalloutAccessoryView和leftCalloutAccessoryView)。您可以在viewForAnnotation 中进行该配置。
从 iOS 9 开始,我们可以访问 detailCalloutAccessoryView,它将标注的副标题替换为可能具有丰富视觉效果的视图,同时仍然享受标注气泡的自动呈现(使用自动布局使这更容易)。
例如,这里有一个标注,它使用 MKSnapshotter 为细节标注附件中的图像视图提供图像,如 WWDC 2015 视频 What's New in MapKit 中所示:
您可以通过以下方式实现:
class SnapshotAnnotationView: MKPinAnnotationView {
override var annotation: MKAnnotation? { didSet { configureDetailView() } }
override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
configure()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
configure()
}
}
private extension SnapshotAnnotationView {
func configure() {
canShowCallout = true
configureDetailView()
}
func configureDetailView() {
guard let annotation = annotation else { return }
let rect = CGRect(origin: .zero, size: CGSize(width: 300, height: 200))
let snapshotView = UIView()
snapshotView.translatesAutoresizingMaskIntoConstraints = false
let options = MKMapSnapshotter.Options()
options.size = rect.size
options.mapType = .satelliteFlyover
options.camera = MKMapCamera(lookingAtCenter: annotation.coordinate, fromDistance: 250, pitch: 65, heading: 0)
let snapshotter = MKMapSnapshotter(options: options)
snapshotter.start { snapshot, error in
guard let snapshot = snapshot, error == nil else {
print(error ?? "Unknown error")
return
}
let imageView = UIImageView(frame: rect)
imageView.image = snapshot.image
snapshotView.addSubview(imageView)
}
detailCalloutAccessoryView = snapshotView
NSLayoutConstraint.activate([
snapshotView.widthAnchor.constraint(equalToConstant: rect.width),
snapshotView.heightAnchor.constraint(equalToConstant: rect.height)
])
}
}
当然,您随后会将该注释视图注册到您的地图中,并且根本不需要 mapView(_:viewFor:):
mapView.register(SnapshotAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)
如果您正在寻找对标注进行更彻底的重新设计或需要支持 iOS 9 之前的版本,则需要做更多的工作。该过程需要 (a) 禁用默认标注; (b) 当用户点击现有的注释视图(即地图上的可视图钉)时添加您自己的视图。
然后复杂性出现在标注的设计中,您必须在其中绘制所有想要显示的内容。例如。如果你想画一个气泡来产生呼出的弹出感觉,你必须自己做。但是,如果您熟悉如何绘制形状、图像、文本等,您应该能够渲染实现所需 UX 的标注:
只需将视图添加为注释视图本身的子视图,并相应地调整其约束:
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
let calloutView = ...
calloutView.translatesAutoresizingMaskIntoConstraints = false
calloutView.backgroundColor = UIColor.lightGray
view.addSubview(calloutView)
NSLayoutConstraint.activate([
calloutView.bottomAnchor.constraint(equalTo: view.topAnchor, constant: 0),
calloutView.widthAnchor.constraint(equalToConstant: 60),
calloutView.heightAnchor.constraint(equalToConstant: 30),
calloutView.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: view.calloutOffset.x)
])
}
有关创建您自己的标注视图的示例,请参阅https://github.com/robertmryan/CustomMapViewAnnotationCalloutSwift。这仅添加了两个标签,但它说明了您可以绘制任何您想要的形状的气泡,使用约束来决定标注的大小等。