我是一位经验丰富的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
答案 0 :(得分:0)
您是否可以尝试以下方法作为解决方法:
1)删除mapView中的代码:didAddAnnotationViews:
2)地图被移动或收缩时的拾取(我认为这是你认为是滚动的权利吗?) - 我用手势而不是mapView regionChanged这样做,因为我有不好的经历,例如不明原因的行为后者。
//recognise the paning gesture to fire the didDragMap method
UIPanGestureRecognizer* panRec = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(didDragMap:)];
[panRec setDelegate:self];
[self.mapView addGestureRecognizer:panRec];
//recognise the pinching gesture to fire the didPinchMap method
UIPinchGestureRecognizer *pinchRec = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(didPinchMap:)];
[pinchRec setDelegate:self];
//[pinchRec setDelaysTouchesBegan:YES];
[self.mapView addGestureRecognizer:pinchRec];
//recognise the doubleTap
UITapGestureRecognizer *doubleTapRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didPinchMap:)];
[doubleTapRec setDelegate:self];
doubleTapRec.numberOfTapsRequired = 2;
[self.mapView addGestureRecognizer:doubleTapRec];
3)编写一个自定义的“plotAnnotations”函数来显示您的注释。此函数将循环遍历所有注释,并将它们保存在由您的位置ID DESC排序的数组“annotationsToShow”中,以便最后添加location_id -1。使用[self.mapView addAnnotations:annotationsToShow];显示它们。
4)在gestureRecognizer函数中调用plotAnnotations
- (void)didDragMap:(UIGestureRecognizer*)gestureRecognizer {
if (gestureRecognizer.state == UIGestureRecognizerStateEnded){
[self plotAnnotations];
}
}
- (void)didPinchMap:(UIGestureRecognizer*)gestureRecognizer {
if (gestureRecognizer.state == UIGestureRecognizerStateEnded){
[self plotAnnotations];
}
}
5)您可能需要删除所有注释,然后才能在3)中显示新注释[self.mapView removeAnnotations:annotationsToRemove];