ios – 自定义MKAnnotation标注视图?

我有一个MKPointAnnotation:
let ann = MKPointAnnotation()
self.ann.coordinate = annLoc
self.ann.title = "Customize me"
self.ann.subtitle = "???"
self.mapView.addAnnotation(ann)

看起来像这样:

如何自定义此标注视图以创建自己的视图而不是预定义视图?

解决方法

应该首先注意,通过简单地调整系统提供的标注的属性,但可以自定义右侧和左侧的附件(通过rightCalloutAccessoryView和leftCalloutAccessoryView),可以实现对标注的最简单更改.您可以在viewForAnnotation中执行该配置.

在iOS 9中,我们现在可以访问detailCalloutAccessoryView,它可以通过潜在的视觉丰富的视图来替换标注的副标题,同时仍然享受标注气泡的自动再现(使用自动布局使此更容易).

例如,这是一个标注,使用MKSnapshotter为详细调用附件中的图像视图提供图像,如WWDC 2015 video What’s New in MapKit所示:

您可以通过以下方式实现此目的:

func mapView(mapView: MKMapView,viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation is MKUserLocation {
        return nil
    }

    let identifier = "MyCustomAnnotation"

    var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)
    if annotationView == nil {
        annotationView = MKPinAnnotationView(annotation: annotation,reuseIdentifier: identifier)
        annotationView?.canShowCallout = true
    } else {
        annotationView!.annotation = annotation
    }

    configureDetailView(annotationView!)

    return annotationView
}

func configureDetailView(annotationView: MKAnnotationView) {
    let width = 300
    let height = 200

    let snapshotView = UIView()
    let views = ["snapshotView": snapshotView]
    snapshotView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[snapshotView(300)]",options: [],metrics: nil,views: views))
    snapshotView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[snapshotView(200)]",views: views))

    let options = MKMapSnapshotOptions()
    options.size = CGSize(width: width,height: height)
    options.mapType = .SatelliteFlyover
    options.camera = MKMapCamera(lookingAtCenterCoordinate: annotationView.annotation!.coordinate,fromDistance: 250,pitch: 65,heading: 0)

    let snapshotter = MKMapSnapshotter(options: options)
    snapshotter.startWithCompletionHandler { snapshot,error in
        if snapshot != nil {
            let imageView = UIImageView(frame: CGRect(x: 0,y: 0,width: width,height: height))
            imageView.image = snapshot!.image
            snapshotView.addSubview(imageView)
        }
    }

    annotationView.detailCalloutAccessoryView = snapshotView
}

如果您正在寻找更加激进的重新设计标注,或者需要在9之前支持iOS版本,则需要更多的工作.该过程需要(a)禁用默认标注;和(b)当用户点击现有注释视图(即地图上的可视引脚)时,添加您自己的视图.

复杂度然后来自于标注的设计,您必须绘制您想要的所有内容.例如.如果你想画一个泡泡来产生呼啸而过的感觉,你必须自己去做.但是,对于如何绘制形状,图像,文本等的熟悉程度,您应该能够呈现实现所需UX的标注:

只需将视图添加为注释视图本身的子视图,并相应地调整其约束:

func mapView(mapView: MKMapView,didSelectAnnotationView view: MKAnnotationView) {
    let calloutView = ...
    calloutView.translatesAutoresizingMaskIntoConstraints = false
    calloutView.backgroundColor = UIColor.lightGrayColor()
    view.addSubview(calloutView)

    NSLayoutConstraint.activateConstraints([
        calloutView.bottomAnchor.constraintEqualToAnchor(view.topAnchor,constant: 0),calloutView.widthAnchor.constraintEqualToConstant(60),calloutView.heightAnchor.constraintEqualToConstant(30),calloutView.centerXAnchor.constraintEqualToAnchor(view.centerXAnchor,constant: view.calloutOffset.x)
    ])
}

有关创建自己的标注视图的示例,请参阅https://github.com/robertmryan/CustomMapViewAnnotationCalloutSwift.这只添加了两个标签,但它说明了一个事实,您可以绘制任何想要的形状,使用约束来规定标注的大小等.

以上是来客网为你收集整理的ios – 自定义MKAnnotation标注视图?全部内容,希望文章能够帮你解决ios – 自定义MKAnnotation标注视图?所遇到的程序开发问题。

如果觉得来客网网站内容还不错,欢迎将来客网网站推荐给程序员好友。