Swift - 用于 MKAnnotationView 地图引脚的自定义 UIView

Mic*_*cro 0 uiview mapkit ios swift

如何设置自定义视图MKAnnotationView?我希望我的地图图钉通过UIView. 该UIView子类中可能有我想要自定义的其他视图。

网上有很多关于如何设置注释图像的示例,但没有如何实际更改该注释:

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

        let annotationIdentifier = "AnnotationIdentifier"
        var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(annotationIdentifier)

        if annotationView == nil {
            annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
            annotationView!.canShowCallout = true
        }
        else {
            annotationView!.annotation = annotation
        }

        let pinImage = UIImage(named: "customPinImage")
        annotationView!.image = pinImage

       return annotationView   
    }
Run Code Online (Sandbox Code Playgroud)

Ste*_*cht 7

MKAnnotationView 是 UIView 的一个子类,它本身可以被子类化。

所以你只需要继承 MKAnnotationView。

自定义子视图

这是一个显示蓝色三角形的简单示例。由于您提到 UIView 自定义子类中应该有其他视图,因此我添加了一个应该显示数字的标签。

class CustomAnnotationView: MKAnnotationView {
    private let annotationFrame = CGRect(x: 0, y: 0, width: 40, height: 40)
    private let label: UILabel

    override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
        self.label = UILabel(frame: annotationFrame.offsetBy(dx: 0, dy: -6))
        super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
        self.frame = annotationFrame
        self.label.font = UIFont.systemFont(ofSize: 24, weight: .semibold)
        self.label.textColor = .white
        self.label.textAlignment = .center
        self.backgroundColor = .clear
        self.addSubview(label)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) not implemented!")
    }

    public var number: UInt32 = 0 {
        didSet {
            self.label.text = String(number)
        }
    }

    override func draw(_ rect: CGRect) {
        guard let context = UIGraphicsGetCurrentContext() else { return }

        context.beginPath()
        context.move(to: CGPoint(x: rect.midX, y: rect.maxY))
        context.addLine(to: CGPoint(x: rect.maxX, y: rect.minY))
        context.addLine(to: CGPoint(x: rect.minX, y: rect.minY))
        context.closePath()

        UIColor.blue.set()
        context.fillPath()
    }

}
Run Code Online (Sandbox Code Playgroud)

MKMapViewDelegate 方法

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

    guard annotation is MKPointAnnotation else { return nil }

    let customAnnotationView = self.customAnnotationView(in: mapView, for: annotation)
    customAnnotationView.number = arc4random_uniform(10)
    return customAnnotationView
}
Run Code Online (Sandbox Code Playgroud)

自定义注释视图

private func customAnnotationView(in mapView: MKMapView, for annotation: MKAnnotation) -> CustomAnnotationView {
    let identifier = "CustomAnnotationViewID"

    if let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as? CustomAnnotationView {
        annotationView.annotation = annotation
        return annotationView
    } else {
        let customAnnotationView = CustomAnnotationView(annotation: annotation, reuseIdentifier: identifier)
        customAnnotationView.canShowCallout = true
        return customAnnotationView
    }
}
Run Code Online (Sandbox Code Playgroud)

结果

结果如下所示:

自定义注释视图