Updating MKannotation image without flashing

前端 未结 3 1687
孤独总比滥情好
孤独总比滥情好 2021-02-06 19:33

I want to update the images of some of my annotations on a mapview every 5 seconds, however I dont\' want to remove and re-add them to the map as this causes them to \'flash\' o

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-06 19:48

    It seems you are using your own custom views for the annotations, in that case you can simply add a "refresh" method to your custom view and call it after you have updated the underlying annotation (ie: a custom view -a derived class from MKAnnotationView- is always attached to a potentially custom "annotation" class that conforms to the MKAnnotation protocol)

    *) CustomAnnotationView.h

    @interface CustomAnnotationView : MKAnnotationView
    {
        ...
    }
    ...
    
    //tell the view to re-read the annotation data it is attached to
    - (void)refresh;
    

    *) CustomAnnotationView.m

    //override super class method
    - (void)setAnnotation:(id )annotation
    {
        [super setAnnotation:annotation];
        ...
        [self refresh];
    }
    - (void)refresh
    {
        ...
        [self setNeedsDisplay]; //if necessary
    }
    

    *) Where you handle the MKMapView and its annotations

    for(CustomAnnotation *annotation in [m_MapView annotations])
    {
        CustomAnnotationView *annotationView = [m_MapView viewForAnnotation:annotation];
        [annotationView refresh];
    }
    

提交回复
热议问题