2016-07-26 6 views
0

に選択解除:私は私にはズームインしたときに、しかしMKAnnotationViewがMKAnnotationViewは、次のコードを使用してMapkKitでクリックされたときに、私はピンに大幅にズームインしようとしています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の注釈が変更された場合、選択した注釈は選択解除されます(削除されようとしています)。

これを修正する方法の1つは、アノテーションの 'diff'置換を行うことです。

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の注釈を追加したり削除したりするのは本当に高価なことがあります。

関連する問題