私は経験豊富な iOS 開発者ですが、これには本当に困惑しました。同時に問題レポートを Apple に提出しています。
MKMapKit マップ (Xcode 4.6) に注釈を追加しています。各注釈は MyAnnotation クラスです。MyAnnotation は、その注釈を追跡するために使用するプロパティ location_id を定義します。
問題は単純です: location_id が -1 の MyAnnotation を他のすべての前に表示したいのです。
これを行うために、MKMapViewDelegate で mapView:didAddAnnotationViews: をオーバーライドしています。
-(void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views {
// Loop through any newly added views, arranging them (z-index)
for (MKAnnotationView* view in views) {
// Check the location ID
if([view.annotation isKindOfClass:[MyAnnotation class]] && [((MyAnnotation*)(view.annotation)).location_id intValue]==-1 ) {
// -1: Bring to front
[[view superview] bringSubviewToFront:view];
NSLog(@"to FRONT: %@",((MyAnnotation*)view.annotation).location_id);
} else {
// Something else: send to back
[[view superview] sendSubviewToBack:view];
NSLog(@"to BACK: %@",((MyAnnotation*)view.annotation).location_id);
}
}
}
これはうまくいきます。マップの中心近くのランダムな場所に注釈を追加する「追加」ボタンがあります。「追加」ボタンを押すたびに、新しい注釈が表示されます。しかし、location_id が -1 の注釈を隠すものは何もありません。
** まで ** スクロールします!
スクロールを開始するとすぐに、すべての注釈が再配置され (z オーダー)、注意深いスタックは適用されなくなります。
本当に紛らわしいのは、以前にアイコンの順序の積み重ねをまったく問題なく行ったことがあるということです。この問題をテストするために、まったく新しいシングル ビュー アプリを作成しました。MKAnnotationView アイテムの背面または前面への送信は、スクロールするまでのみ機能します。このスケルトン アプリには、上記のコード以外には何もありません。最新の MapKit フレームワークに何らかのバグがあるのではないかと考えています。
元の問題は、ユーザーがスクロールしたときに新しい注釈を追加しようとすることでした (mapView:regionDidChangeAnimated:)。注釈が追加されます。mapView:didAddAnnotationViews: コードが起動します。次に、フレームワーク内の目に見えない手によって注文がスクランブルされます (おそらく、スクロールが完了すると)。
興味がある場合は、ここに私の viewForAnnotation があります。
-(MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
// Is this an A91Location?
if([annotation isKindOfClass:[MyAnnotation class]]){
MyAnnotation* ann=(MyAnnotation*)annotation;
NSLog(@"viewForAnnotation with A91Location ID %@",ann.location_id);
if([ann.location_id intValue]==-1){
// If the ID is -1, use a green pin
MKPinAnnotationView* green_pin=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
green_pin.pinColor=MKPinAnnotationColorGreen;
green_pin.enabled=NO;
return green_pin;
} else {
// Otherwise, use a default (red) pin
MKPinAnnotationView* red_pin=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
red_pin.enabled=NO;
return red_pin;
}
}
// Everything else
return nil;
}
そして私のクラス:
@interface MyAnnotation : NSObject <MKAnnotation>
@property (strong, nonatomic, readonly) NSNumber* location_id;
@property (strong, nonatomic, readonly) NSString* name;
@property (strong, nonatomic, readonly) NSString* description;
@property (nonatomic) CLLocationCoordinate2D coordinate;
-(id) initWithID:(NSNumber*)location_id name: (NSString*) name description:(NSString*) description location:(CLLocationCoordinate2D) location;
// For MKAnnotation protocol... return name and description, respectively
-(NSString*)title;
-(NSString*)subtitle;
@end