如何为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
    }

最佳答案

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()
    }

}

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
}

自定义注释视图
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
    }
}

结果
结果如下:
ios - Swift-用于MKAnnotationView map 图钉的自定义UIView-LMLPHP

关于ios - Swift-用于MKAnnotationView map 图钉的自定义UIView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51180853/

10-12 00:16
查看更多