change annotation image after its created

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-19 07:58:15

问题


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?


回答1:


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.

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"];

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



来源:https://stackoverflow.com/questions/10439335/change-annotation-image-after-its-created

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!