gpt4 book ai didi

ios - 有什么方法可以重置 dequeueReusableAnnotationViewWithIdentifier 中使用的队列吗?

转载 作者:行者123 更新时间:2023-11-29 02:42:29 25 4
gpt4 key购买 nike

我在 map View 中添加自定义注释。基于应用程序的内部模型,注释可以改变颜色和标题(尽管位置可能不会改变)。我在方法中使用 dequeueReusableAnnotationViewWithIdentifier

- (MKAnnotationView *)mapView:(MKMapView *) viewForAnnotation:(id <MKAnnotation>)

奇怪的是,当模型改变时,注释被“刷新”以使用正确的标题和颜色(我只是使用 removeAnnotations: 并添加新的),但后来玩 map 上一些颜色错误的旧注释已出队。每次模型更改时,我都使用不同的标识符。

代码如下:

- (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id <MKAnnotation>)annotation{
// in case it's the user location, we already have an annotation, so just return nil
if ([annotation isKindOfClass:[MKUserLocation class]]){
return nil;
}
if ([annotation isKindOfClass:[APGSAnnotation class]]){
APGSAnnotation *gsn = (APGSAnnotation*) annotation;
NSString *GSAnnotationIdentifier = [NSString stringWithFormat:@"gid_%lu_%@", (unsigned long)gsn.gs.gID, self.car.energyType];

MKAnnotationView *markerView = [theMapView dequeueReusableAnnotationViewWithIdentifier:GSAnnotationIdentifier];
if (markerView == nil) {
MKAnnotationView *annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:GSAnnotationIdentifier];
annotationView.canShowCallout = YES;
annotationView.image = [self customizeAnnotationImage:gsn.gs];
annotationView.opaque = NO;

UIImageView *sfIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:gsn.logo]];
annotationView.leftCalloutAccessoryView = sfIconView;

// http://stackoverflow.com/questions/8165262/mkannotation-image-offset-with-custom-pin-image
annotationView.centerOffset = CGPointMake(0,-annotationView.image.size.height/2);


UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:nil action:nil forControlEvents:UIControlEventTouchUpInside];
annotationView.rightCalloutAccessoryView = rightButton;

return annotationView;
}else{
markerView.annotation = annotation;
return markerView;
}
}
return nil;
}

和自定义方法

- (UIImage*)customizeAnnotationImage:(APGS*)gs{
UIImage *markerImage;

if (gs.gID == self.best.gID) {
markerImage = [UIImage imageNamed:@"marker_red.png"];
}else if (gs.type == k1){
markerImage = [UIImage imageNamed:@"marker_blue.png"];
}else if (gs.type == k2){
markerImage = [UIImage imageNamed:@"marker_green.png"];
}else if (gs.type == k3){
markerImage = [UIImage imageNamed:@"marker_purple.png"];
}else if (gs.type == k4){
markerImage = [UIImage imageNamed:@"marker_brown.png"];
}
UIImage *logoImage = [UIImage imageNamed:gs.logo];
// size the flag down to the appropriate size
CGRect resizeRect;
resizeRect.size = markerImage.size;
CGSize maxSize = CGRectInset(self.view.bounds, kAnnotationPadding, kAnnotationPadding).size;

maxSize.height -= self.navigationController.navigationBar.frame.size.height + kCallOutHeight;

if (resizeRect.size.width > maxSize.width)
resizeRect.size = CGSizeMake(maxSize.width, resizeRect.size.height / resizeRect.size.width * maxSize.width);

if (resizeRect.size.height > maxSize.height)
resizeRect.size = CGSizeMake(resizeRect.size.width / resizeRect.size.height * maxSize.height, maxSize.height);

resizeRect.origin = CGPointMake(0.0, 0.0);
float initialWidth = resizeRect.size.width;

UIGraphicsBeginImageContextWithOptions(resizeRect.size, NO, 0.0f);
[markerImage drawInRect:resizeRect];
resizeRect.size.width = resizeRect.size.width/2;
resizeRect.size.height = resizeRect.size.height/2;

resizeRect.origin.x = resizeRect.origin.x + (initialWidth - resizeRect.size.width)/2;
resizeRect.origin.y = resizeRect.origin.y + kLogoHeightPadding;

[logoImage drawInRect:resizeRect];


// Create string drawing context
UIFont *font = [UIFont fontWithName:@"DBLCDTempBlack" size:11.2];
NSString * num = [NSString stringWithFormat:@"%4.3f",[gs getL]];
NSDictionary *textAttributes = @{NSFontAttributeName: font,
NSForegroundColorAttributeName: [UIColor whiteColor]};

CGSize textSize = [num sizeWithAttributes:textAttributes];

NSStringDrawingContext *drawingContext = [[NSStringDrawingContext alloc] init];

//adjust center
if (resizeRect.size.width - textSize.width > 0) {
resizeRect.origin.x += (resizeRect.size.width - textSize.width)/2;
}else{
resizeRect.origin.x -= (resizeRect.size.width - textSize.width)/2;
}

resizeRect.origin.y -= kTextPadding;
[num drawWithRect:resizeRect
options:NSStringDrawingUsesLineFragmentOrigin
attributes:textAttributes
context:drawingContext];

UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resizedImage;
}

根据汽车类型,注释应更改颜色。

最佳答案

根据标题,答案是否定的:)

通常,您会将 View 出列并重置 View 的所有相关属性

解决方法:

您可以考虑的是:如果 View 变化太多,您可以切换到不同的 reusingIdentifier,从而切换队列并“绕过”缓存的 View

关于ios - 有什么方法可以重置 dequeueReusableAnnotationViewWithIdentifier 中使用的队列吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25591038/

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