cocoa - Core Graphics: Drawing along a path with a normal gradient -
there number of resources here , elsewhere on web regarding how draw gradient - fill or stroke.
however, afaict, none addresses following requirement: how draw path normal gradient, whereby normal means orthogonal path. net effect toothpaste or tube when applied dark->light->dark linear gradient. here idea in case of round rectangle:
round-rect tube http://muys.net/cadre_blanc.png
(this hand drawn , corners not good).
in specific case of round rect, think can achieve effect 4 linear gradients (the sides) , 4 radial gradients (the corners). there better?
is there easy solution path?
the "easy" solution can think of stroke path multiple times, reducing stroke width , changing color each time, simulate gradient.
obviously, expensive operation complex paths want cache result if possible.
#define rkrandom(x) (arc4random() % ((nsuinteger)(x) + 1)) @implementation strokeview - (void)drawrect:(nsrect)rect { nsrect bounds = self.bounds; //first draw using core graphics calls cgcontextref c = [[nsgraphicscontext currentcontext] graphicsport]; cgmutablepathref path = cgpathcreatemutable(); cgpathmovetopoint(path, null, nsmidx(bounds), nsmidy(bounds)); cgcontextsetmiterlimit(c,90.0); cgcontextsetlinejoin(c, kcglinejoinround); cgcontextsetlinecap(c, kcglinecapround); for(nsuinteger f = 0; f < 20; f++) { cgpathaddcurvetopoint( path, null, (cgfloat)rkrandom((nsinteger)nswidth(bounds)) + nsminx(bounds), (cgfloat)rkrandom((nsinteger)nsheight(bounds)) + nsminy(bounds), (cgfloat)rkrandom((nsinteger)nswidth(bounds)) + nsminx(bounds), (cgfloat)rkrandom((nsinteger)nsheight(bounds)) + nsminy(bounds), (cgfloat)rkrandom((nsinteger)nswidth(bounds)) + nsminx(bounds), (cgfloat)rkrandom((nsinteger)nsheight(bounds)) + nsminy(bounds) ); } for(nsinteger = 0; < 8; i+=2) { cgcontextsetlinewidth(c, 8.0 - (cgfloat)i); cgfloat tint = (cgfloat)i * 0.15; cgcontextsetrgbstrokecolor ( c, 1.0, tint, tint, 1.0 ); cgcontextaddpath(c, path); cgcontextstrokepath(c); } cgpathrelease(path); //now draw using cocoa drawing nsbezierpath* cocoapath = [nsbezierpath bezierpathwithroundedrect:nsinsetrect(self.bounds, 20.0, 20.0) xradius:10.0 yradius:10.0]; for(nsinteger = 0; < 8; i+=2) { [cocoapath setlinewidth:8.0 - (cgfloat)i]; cgfloat tint = (cgfloat)i * 0.15; nscolor* color = [nscolor colorwithcalibratedred:tint green:tint blue:1.0 alpha:1.0]; [color set]; [cocoapath stroke]; } } @end
Comments
Post a Comment