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

当应用程序进入后台时,如何防止在 SwiftUI 中取消选择 MKMapView 上的注释

如何解决当应用程序进入后台时,如何防止在 SwiftUI 中取消选择 MKMapView 上的注释

在我的 SwiftUI 视图中,我使用通过 UIViewRepresentable 协议包装的 MKMapView。在地图上我有一个注释,当我选择它并将应用程序最小化到后台时,注释会自动再次取消选择。如果使用 UIKit,则此行为会有所不同,在同一情况下,注释不会变为未选中状态。是否可以使用 SwiftUI 实现相同的行为?

这是我的 SwiftUI 地图代码

struct MapView: UIViewRepresentable {
    
    private var mapView = MKMapView()
    
    let coordinates = CLLocationCoordinate2D(latitude: 40.785091,longitude: -73.968285)
    
    func makeUIView(context: UIViewRepresentableContext<MapView>) -> MKMapView {
        mapView.delegate = context.coordinator
        mapView.showsUserLocation = true
        mapView.userTrackingMode = .follow
        
        let region = MKCoordinateRegion(center: coordinates,latitudinalMeters: 1000,longitudinalMeters: 1000)
        mapView.setRegion(region,animated: false)
        
        return mapView
    }
    
    func updateUIView(_ uiView: MKMapView,context: UIViewRepresentableContext<MapView>) {
        addAnnotations()
    }
    
    func makeCoordinator() -> MapView.Coordinator {
        return Coordinator(self)
    }
    
    private func addAnnotations() {
        mapView.removeAnnotations(mapView.annotations)
        let annotation = MKPointAnnotation()
        annotation.coordinate = coordinates
        mapView.addAnnotation(annotation)
        
    }
}

extension MapView {
    
    final class Coordinator: NSObject,MKMapViewDelegate {
        let parent: MapView
        
        init(_ parent: MapView) {
            self.parent = parent
        }
        
        func mapView(_ mapView: MKMapView,didSelect view: MKAnnotationView) {
            debugPrint("Select annotation")
        }
        
        func mapView(_ mapView: MKMapView,diddeselect view: MKAnnotationView) {
            debugPrint("deselect annotation")
        }
    }
}

解决方法

我发现每次app回到前台时都会调用UIViewRepresantable方法(为什么?)这意味着每次发生这种情况时都会删除注释并重新添加。所以基本上您需要做的就是确保您尝试添加到地图的注释尚未显示在那里。如果新注释不包含某些已显示的注释,则您必须将其删除。

我是这样处理的:

func updateUIView(_ uiView: MKMapView,context: UIViewRepresentableContext<SCMapView>) {
        createAnnotations(uiView)
}
    
private func createAnnotations(_ view: MKMapView) {
    let oldAnnotations = view.annotations
    var newAnnotations: [PostAnnotation] = viewStore.annotations
        
    if !oldAnnotations.isEmpty {
        for annotation in oldAnnotations {
            if let postAnnotation = annotation as? PostAnnotation {
                if !newAnnotations.contains(where: { $0.post.id == postAnnotation.post.id }) {
                     view.removeAnnotation(postAnnotation)
                } else {
                     newAnnotations.removeAll(where: { $0.post.id == postAnnotation.post.id })
                }
             }
         }
     }
        
    if !newAnnotations.isEmpty {
        view.addAnnotations(newAnnotations)
    }
 }

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

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?