gpt4 book ai didi

xcode - MapKit 在 XCode 4.6 中滚动 map 时更改注释 View 顺序

转载 作者:行者123 更新时间:2023-12-03 17:53:00 25 4
gpt4 key购买 nike

我是一名经验丰富的 iOS 开发人员,但这真的让我很难过。我同时向 Apple 提交问题报告。

我正在向 MKMapKit map (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);
}
}
}

这工作得很好。我有一个“添加”按钮,可以向 map 中心附近的随机位置添加注释。每次按下“添加”按钮时,都会出现一个新注释;但没有任何东西隐藏 location_id 为 -1 的注释。

** 直到 ** 我滚动!

一旦我开始滚动,我的所有注释都会重新排列(z 顺序)并且我小心的堆叠不再适用。

真正令人困惑的是,我以前做过图标顺序堆叠,没有任何问题。我创建了一个全新的单一 View 应用程序来测试这个问题;将 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;


}

还有我的类(class):
@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

最佳答案

您能否尝试以下解决方法:

1) 在 mapView:didAddAnnotationViews 中删除您的代码:

2)在移动或挤压 map 时拾取(我认为这就是您认为的滚动对吗?)-我使用手势而不是 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] 中显示新注释之前,您可能需要删除所有注释;

关于xcode - MapKit 在 XCode 4.6 中滚动 map 时更改注释 View 顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16533638/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com