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

SwiftUI:点击注释后显示 ActionSheet

如何解决SwiftUI:点击注释后显示 ActionSheet

我创建了一个 UIViewRepresantable 来在 SwiftUI 中显示地图。我想在点击注释后显示一个 ActionSheet。在 Coordinator 中,我尝试使用 calloutAccessoryControlTapped 方法。现在我只是想打印“注释被点击”,但这不起作用。有人知道我可能遗漏了什么吗?


struct MapModel: UIViewRepresentable {
    
    let view = UIView()
    let map = MKMapView()
    
    let yedikuleShelter = Shelterannotation(title: "Yedikule Hayvan Barınağı",coordinate: CLLocationCoordinate2D(latitude: 40.9907822322955,longitude: 28.920995484659933))
    
    func makeUIView(context: Context) -> UIView {
        
        view.addSubview(map)
        
        map.translatesAutoresizingMaskIntoConstraints = false
        
        NSLayoutConstraint.activate([
            map.widthAnchor.constraint(equalTo: view.widthAnchor),map.heightAnchor.constraint(equalTo: view.heightAnchor),map.centerXAnchor.constraint(equalTo: view.centerXAnchor),map.centerYAnchor.constraint(equalTo: view.centerYAnchor)
        ])
        
        return view
    }
    
    class MapViewCoordinator: NSObject,MKMapViewDelegate {
        var mapModelController: MapModel
        
        init(_ control: MapModel) {
            self.mapModelController = control
        }
        
        func mapView(_ mapView: MKMapView,viewFor
                        annotation: MKAnnotation) -> MKAnnotationView?{
            let annotationView = MKAnnotationView(annotation: annotation,reuseIdentifier: "customView")
            //Custom Image Icon
            annotationView.image = #imageLiteral(resourceName: "AnbulancePin")
            annotationView.frame.size = CGSize(width: 50,height: 50)
            return annotationView
        }
        
        func mapView(_ mapView: MKMapView,didSelect view: MKAnnotationView) {
            let btn = UIButton(type: .detaildisclosure)
            view.canShowCallout = true
            view.rightCalloutAccessoryView = btn 
        }
        
        //display alert once annotation is tapped
        
        func mapView(_ mapView: MKMapView,annotationView view: MKAnnotationView,calloutAccessoryControlTapped control: UIControl) {
            print("annotation is tapped")  
        }
        
    }
    
    class Shelterannotation: NSObject,MKAnnotation {
        let title: String?
        let coordinate: CLLocationCoordinate2D
        init(title: String?,coordinate: CLLocationCoordinate2D) {
            self.title = title
            self.coordinate = coordinate
        }
    }  
    
    func updateUIView(_ uiView: UIView,context: Context) {
        let coordinate = CLLocationCoordinate2D(latitude: 41.01224,longitude: 28.976018)
        let span = MKCoordinateSpan(latitudeDelta: 0.7,longitudeDelta: 0.7)
        map.setRegion(MKCoordinateRegion(center: coordinate,span: span),animated: true)
        
        map.delegate = context.coordinator
        map.addAnnotation(yedikuleShelter)
        
    }
    
    func makeCoordinator() -> MapViewCoordinator {
        MapViewCoordinator(self)
    }    
    
}

解决方法

您的 MapViewCoordinator 嵌套在 MapModel 中。

struct MapModel: UIViewRepresentable {

  let view = UIView()
  let map = MKMapView()

  let yedikuleShelter = ShelterAnnotation(title: "Yedikule Hayvan Barınağı",coordinate: CLLocationCoordinate2D(latitude: 40.9907822322955,longitude: 28.920995484659933))


  func makeUIView(context: Context) -> UIView {
     view.addSubview(map)
    
     map.translatesAutoresizingMaskIntoConstraints = false
    
     NSLayoutConstraint.activate([
        map.widthAnchor.constraint(equalTo: view.widthAnchor),map.heightAnchor.constraint(equalTo: view.heightAnchor),map.centerXAnchor.constraint(equalTo: view.centerXAnchor),map.centerYAnchor.constraint(equalTo: view.centerYAnchor)
        
     ])
    
     map.delegate = context.coordinator
    
     return view
  }

  func updateUIView(_ uiView: UIView,context: Context) {
    let coordinate = CLLocationCoordinate2D(latitude: 41.01224,longitude: 28.976018)
    let span = MKCoordinateSpan(latitudeDelta: 0.7,longitudeDelta: 0.7)
    map.setRegion(MKCoordinateRegion(center: coordinate,span: span),animated: true)
    map.addAnnotation(yedikuleShelter)
  }

  func makeCoordinator() -> MapViewCoordinator {
    MapViewCoordinator()
  }
}


class MapViewCoordinator: NSObject,MKMapViewDelegate {

  func mapView(_ mapView: MKMapView,viewFor
                annotation: MKAnnotation) -> MKAnnotationView? {
     let annotationView = MKAnnotationView(annotation: annotation,reuseIdentifier: "customView")
    //Custom Image Icon
    annotationView.image = #imageLiteral(resourceName: "AnbulancePin")
    annotationView.frame.size = CGSize(width: 50,height: 50)
    return annotationView
  }

  func mapView(_ mapView: MKMapView,didSelect view: MKAnnotationView) {
    view.canShowCallout = true
    view.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
    print("Uday")
  }

  func mapView(_ mapView: MKMapView,annotationView view: MKAnnotationView,calloutAccessoryControlTapped control: UIControl) {
    print("annotation is tapped")
  }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。