如何使用自定义MKAnnotation启动自定义MKAnnotationView?

问题描述

我正在尝试将自定义注释视图与自定义注释相关联。我该怎么做?

在这里设置注释视图,并将其与自定义注释相关联:

func mapView(_ mapView: MKMapView,viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        guard !annotation.isKind(of: MKUserLocation.self) else {
            // Make a fast exit if the annotation is the `MKUserLocation`,as it's not an annotation view we wish to customize.
            return nil
        }
        
        var annotationView: MKAnnotationView?
        
        if let annotation = annotation as? HangAnnotation {
            annotationView = setupHangAnnotationView(for: annotation,on: mapView)
        } 

        return annotationView
    }
    private func setupHangAnnotationView(for annotation: HangAnnotation,on mapView: MKMapView) -> MKAnnotationView {
        
        let hangAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: K.identifiers.hangViewIdentifier,for: annotation) as? HangAnnotationView
            hangAnnotationView?.delegate = self
            hangAnnotationView?.canShowCallout = false
            return hangAnnotationView!
    }

(注意:我知道自定义注释已成功创建,因为可以在上面的setupHangAnnotationView()中访问其数据成员)

在这里,我尝试使用自定义注释初始化创建的AnnotationView:

class HangAnnotationView: MKAnnotationView {

     override init(annotation: MKAnnotation?,reuseIdentifier: String?) {
        hangButton = UIButton(frame: annotationFrame)
        super.init(annotation: annotation,reuseIdentifier: reuseIdentifier)

您会看到所需的类型是覆盖init的MKAnnotation,那么如何将自定义注释传递给初始化程序?我超级受限制,因此不胜感激!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)