繁体   English   中英

如何随机显示注释

[英]How to show annotations randomly

我想在每个图钉上显示不同的注释,并将我的图像命名为“ 01.png”至“ 08.png”。 我将整数设置为1到8之间的一个随机数

integer = arc4random() % 8 + 1;

然后设置方法-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:{id)annotation

if(annotation == mapView.userLocation){
    return nil;
}
NSString *identifier = @"Store";

MKAnnotationView *result = (MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];

if(result == nil){
    result = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
} else {
    result.annotation = annotation;
}

NSString * imageName = [NSString stringWithFormat:@"0%i",integer];

UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",imageName]];
result.image = image;

return result;

但是mapView显示相同的注释,每次打开应用程序时它将显示不同的注释。

如何在每个图钉上显示不同的注释?

非常感谢你!

您必须将随机化放入注释方法中。 变量只有一个值,直到它被更改为止,每次您从变量中读取时,它不会获得新的随机值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM