ios - Draw CAGradient within MKPolyLineView -
i have problem mkpolylineview. try make color gradient polyline, cagradient doenst work. subclasses mkpolylineview , redrawing in
- (void)drawmaprect:(mkmaprect)maprect zoomscale:(mkzoomscale)zoomscale incontext:(cgcontextref)context uicolor *darker = [uicolor blackcolor]; cgfloat basewidth = self.linewidth / zoomscale; // draw dark colour thicker cgcontextaddpath(context, self.path); cgcontextsetstrokecolorwithcolor(context, darker.cgcolor); cgcontextsetlinewidth(context, basewidth * 1.5); cgcontextsetlinecap(context, self.linecap); cgcontextstrokepath(context); // draw stroke color regular width cgcontextaddpath(context, self.path); cgcontextsetstrokecolorwithcolor(context, self.strokecolor.cgcolor); cgcontextsetlinewidth(context, basewidth); cgcontextsetlinecap(context, self.linecap); cgcontextstrokepath(context); [super drawmaprect:maprect zoomscale:zoomscale incontext:context]; }
but not working (strokecolor = red). ideas how gradient polyline? (highcolor, centercolor, lowcolor)
thanks everyone.
to paint mkpolyline
gradient, can use custom subclass of mkpolylineview
. coregraphics not support stroking path gradient, have
- convert path shape traces paths edge using
cgpathcreatecopybystrokingpath
- clip context shape
- fill using
cgcontextdrawlineargradient
here subclass started:
@interface twogradientpolylineview : mkpolylineview @end @implementation twogradientpolylineview - (void)strokepath:(cgpathref)path incontext:(cgcontextref)context { cgfloat linewidth = cgcontextconvertsizetouserspace(context, (cgsize){self.linewidth, self.linewidth}).width; cgpathref pathtofill = cgpathcreatecopybystrokingpath(path, null, linewidth, self.linecap, self.linejoin, self.miterlimit); cgrect rect = cgpathgetboundingbox(pathtofill); cgcontextaddpath(context, pathtofill); cgpathrelease(pathtofill); cgcontextclip(context); cgfloat gradientlocations[2] = {0.0f, 1.0f}; cgfloat gradientcolors[8] = {1.0f, 0.0f, 0.0f, 0.75f, 1.0f, 1.0f, 0.0f, 0.75f}; cgcolorspaceref colorspace = cgcolorspacecreatedevicergb(); cggradientref gradient = cggradientcreatewithcolorcomponents(colorspace, gradientcolors, gradientlocations, 2); cgcolorspacerelease(colorspace); cgpoint gradientstart = rect.origin; cgpoint gradientend = {cgrectgetmaxx(rect), cgrectgetmaxy(rect)}; cgcontextdrawlineargradient(context, gradient, gradientstart, gradientend, kcggradientdrawsafterendlocation); cggradientrelease(gradient); } @end
here screenshot of path drawn class above:
Comments
Post a Comment