微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决如何使用自定义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 举报,一经查实,本站将立刻删除。