2016-07-26 59 views
0

我想點擊一個MKAnnotationView時MapkKit使用下面的代碼顯著放大一針:MKAnnotationView取消對setRegion

MKCoordinateRegion mapRegion; 
    mapRegion.center = view.annotation.coordinate;; 
    mapRegion.span.latitudeDelta = 0.2; 
    mapRegion.span.longitudeDelta = 0.2; 

    [MKMapView animateWithDuration:0.15 animations:^{ 
     [mapView setRegion:mapRegion animated: YES]; 
    }]; 

但是,每當我在我放大想要的引腳保持選中狀態。有沒有辦法阻止MKAnnotatiotionView被取消選擇,並且函數didDeselectAnnotationView不被調用。

我認爲它可能發生的原因是因爲縮放的mapView正在更新註釋。有沒有辦法來防止這種情況發生?

回答

0

是的,如果[mapView setRegion: ...]導致mapView上的註釋因任何原因而改變,那麼您選擇的註釋將被取消選擇(因爲它將要被移除!)。

解決此問題的一種方法是對您的註釋進行「差異」替換。例如,此刻,你可能有一些代碼,看起來像(斯威夫特表示):

func displayNewMapPins(pinModels: [MyCustomPinModel]) { 
    self.mapView.removeAnnotations(self.mapView.annotations) //remove all of the currently displayed annotations 

    let newAnnotations = annotationModels.map { $0.toAnnotation } //convert 'MyCustomPinModel' to an 'MKAnnotation' 
    self.mapView.addAnnotations(newAnnotations) //put the new annotations on the map 
} 

你想改變它,更是這樣的:

func displayNewMapPins(pinModels: [MyCustomPinModel]) { 
    let oldAnnotations = self.mapView.annotations 
    let newAnnotations = annotationModels.map { $0.toAnnotation } 

    let annotationsToRemove = SomeOtherThing.thingsContainedIn(oldAnnotations, butNotIn: newAnnotations) 
    let annotationsToAdd = SomeOtherThing.thingsContainedIn(newAnnotations, butNotIn: oldAnnotations) 

    self.mapView.removeAnnotations(annotationsToRemove) 
    self.mapView.addAnnotations(annotationsToAdd) 
} 

SomeOtherThing.thingsContainedIn(:butNotIn:)確切實施取決於您的要求,但這是您希望實現的通用代碼結構。

這樣做會提高您的應用程序的性能 - 添加和刪除MKMapView的註釋可能會非常昂貴!