UIBezierPath and gradient iOS -
i have path values , want make gradient.
here code:
cgcontextref context = uigraphicsgetcurrentcontext(); cgcontextsetstrokecolorwithcolor(context, [[uicolor colorwithred:245.0/255.0 green:245.0/255.0 blue:171.0/255.0 alpha:0.6] cgcolor]); cgcontextsetfillcolorwithcolor(context, [[uicolor colorwithred:245.0/255.0 green:245.0/255.0 blue:171.0/255.0 alpha:0.6] cgcolor]); uibezierpath *apath = [uibezierpath bezierpath]; [apath movetopoint:cgpointmake(30.0, 100.0)]; [apath addlinetopoint:cgpointmake(200.0, 120.0)]; [apath addlinetopoint:cgpointmake(300, 210)]; [apath addlinetopoint:cgpointmake(300, 420)]; [apath addlinetopoint:cgpointmake(30, 420.0)]; [apath addlinetopoint:cgpointmake(30, 100.0)]; [apath closepath]; [apath fill];
any pointers figure out issue code?
first - i've created simple arrow bezier path:
uibezierpath* bezierpath = [uibezierpath bezierpath]; [bezierpath movetopoint: cgpointmake(24.5, 1.5)]; [bezierpath addlinetopoint: cgpointmake(2.5, 14.5)]; [bezierpath addlinetopoint: cgpointmake(24.5, 28.5)]; [bezierpath addlinetopoint: cgpointmake(24.5, 1.5)]; [bezierpath closepath]; [[uicolor blackcolor] setstroke]; bezierpath.linewidth = 1; [bezierpath stroke];
then i've drawn simple linear gradient black white:
cgcolorspaceref colorspace = cgcolorspacecreatedevicergb(); cgcontextref context = uigraphicsgetcurrentcontext(); nsarray* simplelineargradientcolors = [nsarray arraywithobjects: (id)[uicolor blackcolor].cgcolor, (id)[uicolor whitecolor].cgcolor, nil]; cgfloat simplelineargradientlocations[] = {0, 1}; cggradientref simplelineargradient = cggradientcreatewithcolors(colorspace, (__bridge cfarrayref)simplelineargradientcolors, simplelineargradientlocations); // bezier drawing uibezierpath* bezierpath = [uibezierpath bezierpath]; [bezierpath movetopoint: cgpointmake(24.5, 1.5)]; [bezierpath addlinetopoint: cgpointmake(2.5, 14.5)]; [bezierpath addlinetopoint: cgpointmake(24.5, 28.5)]; [bezierpath addlinetopoint: cgpointmake(24.5, 1.5)]; [bezierpath closepath]; cgcontextsavegstate(context); [bezierpath addclip]; cgcontextdrawlineargradient(context, simplelineargradient, cgpointmake(2.5, 15), cgpointmake(24.5, 15), 0); cgcontextrestoregstate(context); [[uicolor blackcolor] setstroke]; bezierpath.linewidth = 1; [bezierpath stroke]; cggradientrelease(simplelineargradient); cgcolorspacerelease(colorspace);
that's got:
basically can create linear, radial gradient bunch of settings (locations, colors) , of course should modify code above.
Comments
Post a Comment