ios - MKPolyline / MKPolylineRenderer changing color without remove it -
i working around map app, want ask how change polyline color without remove , add again, found topic https://stackoverflow.com/questions/24226290/mkpolylinerenderer-change-color-without-removing-overlay in stackoverflow not involve question, did not touch line, no need -[mkmapviewdelegate mapview:didselectannotationview:]
so possible that?
edit: want change polyline color smoothly (by shading color - sound animation) if have idea on how animate polyline please tell me too.
complex animations or shading/gradients require creating custom overlay renderer class.
these other answers give ideas how draw gradient polylines , animations require custom overlay renderer well:
- how customize mkpolylineview draw different style lines
- gradient polyline mapkit ios
- draw cagradient within mkpolylineview
apple's breadcrumb sample app has example of custom renderer may find useful.
however, if want update line's color (say blue red), may able follows:
- get reference
mkpolyline
want change. - get reference
mkpolylinerenderer
polyline obtained in step 1. can done calling map view'srendererforoverlay:
instance method (not samemapview:rendererforoverlay:
delegate method. - update renderer's
strokecolor
. - call
invalidatepath
on renderer.
not sure want may able "animate" color going blue red changing color , calling invalidatepath gradually in timed steps.
another important thing make sure rendererforoverlay
delegate method uses line's "current" color in case map view calls delegate method after you've changed renderer's strokecolor
directly.
otherwise, after panning or zooming map, polyline's color change whatever's set in delegate method.
you keep line's current color in class-level variable , use in both delegate method , place want change line's color.
an alternative class-level variable (and better) either use mkpolyline's title
property hold color or custom polyline overlay class (not renderer) color property.
example:
@property (nonatomic, strong) uicolor *linecolor; //if need keep track of multiple overlays, //try using nsmutabledictionary keys //overlay titles , value uicolor. -(void)methodwhereyouoriginallycreateandaddtheoverlay { self.linecolor = [uicolor bluecolor]; //line starts blue mkpolyline *pl = [mkpolyline polylinewithcoordinates:coordinates count:count]; pl.title = @"test"; [mapview addoverlay:pl]; } -(void)methodwhereyouwanttochangelinecolor { self.linecolor = thenewcolor; //get reference mkpolyline (example assumes have 1 overlay)... mkpolyline *pl = [mapview.overlays objectatindex:0]; //get reference 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; }
Comments
Post a Comment