mkmapView работает очень медленно при уменьшении масштаба с использованием кластеризации - PullRequest
0 голосов
/ 27 апреля 2020

я внедряю кластеризацию в моем приложении, когда я уменьшаю масштаб, появляется кластеризация, но производительность приложений очень ухудшается, приложение становится очень медленным. Если он замораживает код ниже:

class StoreClusterView: MKMarkerAnnotationView {


    override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
        super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
        clusteringIdentifier = kMapPinName
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override var annotation: MKAnnotation? {
        willSet {
            clusteringIdentifier = kMapPinName
        }
    }
}

class StoreAnnotationView: MKAnnotationView {//MKAnnotationView

    override var annotation: MKAnnotation? {
        willSet {
            clusteringIdentifier = kMapPinName
            customCalloutView?.removeFromSuperview()
            if let pointAnnotation = newValue as? PointAnnotation {
                displayPriority = pointAnnotation.priority
            }
        }
    }

    override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
        super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
        clusteringIdentifier = kMapPinName
        displayPriority = .defaultHigh
        self.canShowCallout = false
        updateImage()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        self.canShowCallout = false
        updateImage()
    }

    override func prepareForReuse() {
        super.prepareForReuse()
        self.customCalloutView?.removeFromSuperview()

        updateImage()
    }

    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        // if super passed hit test, return the result
        if let parentHitView = super.hitTest(point, with: event) {
            return parentHitView

        } else {
            // test in our custom callout.
            if customCalloutView != nil {
                return customCalloutView!.hitTest(convert(point, to: customCalloutView!), with: event)
            } else {
                return nil

            }
        }
    }
}

extension StoreAnnotationView {

     func updateImage() {
        ....
    } 
}

контроллер представления, который содержит mapView и его функции:

 override func viewDidLoad() {
        super.viewDidLoad()
....
 storeMapView.register(StoreAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultClusterAnnotationViewReuseIdentifier)

                storeMapView.register(StoreClusterView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)

        storeMapView.addAnnotations(StoresManager.shared.clusterManager.annotations)
}

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        if annotation is MKUserLocation {
            return nil
        } else {
            var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: kMapPinName)
            if annotationView == nil {
                if #available(iOS 11.0, *) {
                    annotationView = StoreAnnotationView(annotation: annotation, reuseIdentifier: kMapPinName)
                }
            } else {
                annotationView!.annotation = annotation
            }
            return annotationView
        }
    }

если у кого-то есть идея, что происходит, было бы здорово, спасибо

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...