0

を長押しすると、奇妙な動作が発生することがわかりましたMKPointAnnotation

次のようにピンを作成します。

MKPointAnnotation *activeTRPoint = [[MKPointAnnotation alloc] init];
CLLocation *coord = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
activeTRPoint.coordinate = coord.coordinate;
activeTRPoint.title = @"Title";
activeTRPoint.subtitle = @"subtitle";
//Added tag
[map addAnnotation:activeTRPoint];
[[map viewForAnnotation:activeTRPoint] setTag:1];
UIImage *im = [UIImage imageNamed:@"pin_icon.png"];
[[map viewForAnnotation:activeTRPoint] setImage:im];

そのため、ピンを長押しすると、赤いピン (デフォルト) に変わります。なぜこれが起こるのか分かりますか?

更新: さらなる調査のために viewForAnnotation メソッドを追加しました

- (MKAnnotationView *) mapView:(MKMapView *)mapView1 viewForAnnotation:(id <MKAnnotation>) annotation
{
    if(annotation != map.userLocation){
        // This is not the users location indicator (the blue dot)
        MKAnnotationView *view = [map dequeueReusableAnnotationViewWithIdentifier:@"myAnnotationIdentifier"];
        BOOL isCustomPin = [subtitles containsObject:((MKPointAnnotation*)annotation).subtitle];
        if (!view && isCustomPin == NO) {
            // Creating a new annotation view, in this case it still looks like a pin
            MKPinAnnotationView *annView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotationIdentifier"];
            annView.pinColor = MKPinAnnotationColorGreen;
            view = annView;
            view.canShowCallout = YES; // So that the callout can appear
            UIButton *btnViewVenue = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
            [btnViewVenue addTarget:self action:@selector(pinTouched:) forControlEvents:UIControlEventTouchUpInside];
            view.rightCalloutAccessoryView = btnViewVenue;
            view.enabled = YES;
            view.canShowCallout = YES;
            view.multipleTouchEnabled = NO;
        }else{
            UIImage *im = [UIImage imageNamed:@"pin_icon.png"];
            [view setImage:im];
        }
        return view;
    }else{
        mapView1.userLocation.title = [Lang get:@"USER_CURRENT_LOCATION"];
        mapView1.userLocation.subtitle = @"";
        [mapView1 setTag:999];
        return nil;
    }
}
4

1 に答える 1

2

示されているviewForAnnotationデリゲート メソッドで、実際に作成される (alloc+init) 注釈ビューの唯一のタイプはMKPinAnnotationView.

isCustomPinisの場合YES、そのままのコードは実際には を作成しないMKAnnotationViewため、最終的には次の を参照viewします。

  • が何も返さなかったか、nilまたはdequeue
  • これはMKPinAnnotationViewこれまでに作成されたビューの唯一のタイプであり、デキューはそれらの以前に使用されたものを返すためです。

だからいつisCustomPinですかYES

  • nilマップビューが「デフォルトの赤いピンを表示」(基本的には赤)と解釈するビューを返しているMKPinAnnotationView、または
  • green を返していますMKPinAnnotationView

いずれにせよ、コードは常に を返しますがMKPinAnnotationView、マップ ビューは多くの場合プロパティを無視し、imageに従ってデフォルトのピン イメージを表示しますpinColor


これを解決するには、「カスタム イメージ」ピンの作成と再利用を「グリーン ピン」から分離する必要があります。例えば:

BOOL isCustomPin = [subtitles containsObject:((MKPointAnnotation*)annotation).subtitle];

if (isCustomPin)
{
    //Put dequeue and alloc+init of MKAnnotationView here.
    //NOTE: Use a DIFFERENT re-use identifier for "custom image" pins.
    //Eg. Use "myCustomIdentifier" -- not "myAnnotationIdentifier"
}
else
{
    //Put dequeue and alloc+init of MKPinAnnotationView here.
    //NOTE: Use a DIFFERENT re-use identifier from "custom image" pins.
    //That is, use "myAnnotationIdentifier" -- not "myCustomIdentifier".
}


いくつかの別個の、おそらく関連のない問題:

  • 決定に使用されるロジックisCustomPinは疑わしいようです。annotation外部配列で注釈を探すのではなく、カスタム注釈クラスを作成して、その型であるかどうかを確認する方がよいでしょう。
  • 注釈ビューがデキューされた後、ビューのannotationプロパティを現在のプロパティに設定する必要がありannotationます。そうしないと、ビューは以前の注釈プロパティを使用します。
  • タグを使用する必要はありません (お勧めしません)。ユーザー位置アノテーションはそのクラスによって識別できMKUserLocation、メソッドでは、 のプロパティをpinTouched:使用して選択したアノテーションを取得できます。selectedAnnotationsMKMapView


ちなみに、 を呼び出した後、画像の設定も削除する必要がありますaddAnnotation

于 2013-07-24T13:48:03.293 に答える