创建后更改注释图像 [英] change annotation image after its created

查看:87
本文介绍了创建后更改注释图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义注释,它使用viewForAnnotation委托方法根据注释的类型设置其图像。我只使用1个注释代表汽车移动,并希望在检测到汽车移动和停止时更改图像。除了删除我的注释并重新添加它以引起眨眼之外,我怎么能这样做?

I have a custom annotation that sets its image based on the type of the annotation using the viewForAnnotation delegate method. Im only using 1 annotation that represents a car moving and want to change the image for when the car is detected to be moving and stopped. How could I go about this besides removing my annotation and re-adding it which causes a blink?

推荐答案

无论你在哪里检测到汽车状态已更改,使用 MKMapView 实例方法检索注释的当前视图 viewForAnnotation: 。这与 mapView:viewForAnnotation: 委托方法不同。

Wherever you detect that the car's state has changed, retrieve the annotation's current view using the MKMapView instance method viewForAnnotation:. This is not the same as the mapView:viewForAnnotation: delegate method.

之后获取注释的当前视图,您可以修改其属性,包括图像

After getting the current view for the annotation, you can modify its properties including image.

同时确保 mapView:viewForAnnotation:委托方法具有相同的条件,可根据汽车注释的状态设置 image 。您可能希望将逻辑放在从两个位置调用的公共方法中(状态更改和委托方法),因此代码不会重复。

Also make sure the mapView:viewForAnnotation: delegate method has the same exact condition to set image based on the state of the car annotation. You may want to put the logic in a common method called from both places (where the state changes and the delegate method) so the code isn't duplicated.

例如,状态发生变化,你可能有:

For example, where the state changes, you might have:

//carAnnotation is your id<MKAnnotation> object
MKAnnotationView *av = [mapView viewForAnnotation:carAnnotation];
if (carAnnotation.isMoving)
    av.image = [UIImage imageNamed:@"moving.png"];
else
    av.image = [UIImage imageNamed:@"stopped.png"];

if 声明(或任何逻辑你必须设置 image )是应该在 viewForAnnotation 委托方法中的部分。

The if statement (or whatever logic you have to set image) is the part that should also be in the viewForAnnotation delegate method.

这篇关于创建后更改注释图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆