MKPolyline / MKPolylineRenderer changing color without remove it

前端 未结 2 896
眼角桃花
眼角桃花 2021-01-02 17:37

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/2422629

相关标签:
2条回答
  • 2021-01-02 17:57

    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;
    }
    
    0 讨论(0)
  • 2021-01-02 18:11

    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.

    0 讨论(0)
提交回复
热议问题