MKPolyline / MKPolylineRenderer changing color without remove it

那年仲夏 提交于 2019-12-08 23:53:46

问题


I working around with map app, I want to ask how to change the polyline color without remove and add it again, I found this topic https://stackoverflow.com/questions/24226290/mkpolylinerenderer-change-color-without-removing-overlay in stackoverflow but this is not involve with my question, I did not touch the line, so no need to do with -[MKMapViewDelegate mapView:didSelectAnnotationView:]

So is it possible to do that?

EDIT: What I want to do is change the polyline color smoothly (by shading a color - sound like an animation) If you have any idea on how to animate this polyline please also tell me too. Thanks


回答1:


Complex animations or shading/gradients will probably require creating a custom overlay renderer class.

These other answers give ideas about how to draw gradient polylines and animations will most like require a custom overlay renderer as well:

  • how to customize MKPolyLineView to draw different style lines
  • Gradient Polyline with MapKit ios
  • Draw CAGradient within MKPolyLineView

Apple's Breadcrumb sample app also has an example of a custom renderer which you may find useful.


However, if you just want to update the line's color (say from blue to red), then you may be able to do that as follows:

  1. Get a reference to the MKPolyline you want to change.
  2. Get a reference to the MKPolylineRenderer for the polyline obtained in step 1. This can be done by calling the map view's rendererForOverlay: instance method (not the same as the mapView:rendererForOverlay: delegate method.
  3. Update the renderer's strokeColor.
  4. Call invalidatePath on the renderer.

Not sure what you want but you may be able to "animate" the color going from blue to red by changing the color and calling invalidatePath gradually in timed steps.

Another important thing is to make sure the rendererForOverlay delegate method also uses the line's "current" color in case the map view calls the delegate method after you've changed the renderer's strokeColor directly.

Otherwise, after panning or zooming the map, the polyline's color will change back to whatever's set in the delegate method.

You could keep the line's current color in a class-level variable and use that in both the delegate method and the place where you want to change the line's color.

An alternative to a class-level variable (and probably better) is to either use the MKPolyline's title property to hold its color or a custom polyline overlay class (not renderer) with a color property.

Example:

@property (nonatomic, strong) UIColor *lineColor;
//If you need to keep track of multiple overlays, 
//try using a NSMutableDictionary where the keys are the 
//overlay titles and the value is the UIColor.

-(void)methodWhereYouOriginallyCreateAndAddTheOverlay
{
    self.lineColor = [UIColor blueColor];  //line starts as blue
    MKPolyline *pl = [MKPolyline polylineWithCoordinates:coordinates count:count];
    pl.title = @"test";
    [mapView addOverlay:pl];
}

-(void)methodWhereYouWantToChangeLineColor
{
    self.lineColor = theNewColor;

    //Get reference to MKPolyline (example assumes you have ONE overlay)...
    MKPolyline *pl = [mapView.overlays objectAtIndex:0];

    //Get reference to polyline's renderer...
    MKPolylineRenderer *pr = (MKPolylineRenderer *)[mapView rendererForOverlay:pl];
    pr.strokeColor = self.lineColor;
    [pr invalidatePath];
}

-(MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
    if ([overlay isKindOfClass:[MKPolyline class]]) {
        MKPolylineRenderer *pr = [[MKPolylineRenderer alloc] initWithPolyline:overlay];
        pr.strokeColor = self.lineColor;
        pr.lineWidth = 5;
        return pr;
    }

    return nil;
}



回答2:


Yous hould look at MKOverlayPathRenderer method - invalidatePath.

From the doc, it says:

Call this method when a change in the path information would require you to recreate the overlay’s path. This method sets the path property to nil and tells the overlay renderer to redisplay its contents.

So, at this moment, you should be able to change your drawing color.



来源:https://stackoverflow.com/questions/30186902/mkpolyline-mkpolylinerenderer-changing-color-without-remove-it

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