objective c - CGContextDrawLinearGradient confusion. Need clarification -


i have been experimenting cgcontextdrawlineargradient , terribly confused start point , end point mean? thought mean coordinates on current cgcontext if define start point 0,0 , end point 100,100, square gradient. else altogether cannot connect co-ordinates.

this code have:

- (void)drawrect:(cgrect)rect { // drawing code  cgcontextref current_context = uigraphicsgetcurrentcontext(); cgcontextsavegstate(current_context);  // gradient cgfloat locations[3] = {0.0, 0.5, 1.0}; cgfloat components[12] = {1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0}; cgcolorspaceref colorspace = cgcolorspacecreatedevicergb(); cggradientref gradient = cggradientcreatewithcolorcomponents(colorspace, components, locations, 3); cgpoint startpoint = cgpointmake(0, 0); cgpoint endpoint = cgpointmake(40, 40); cgcontextdrawlineargradient(current_context, gradient, startpoint, endpoint, 0);   // shadow cgcontextsetshadow(current_context, cgsizemake(4,7), 1.0);  // image uiimage *logoimage = [uiimage imagenamed:@"logo.png"]; [logoimage drawinrect:bounds]; cgcontextrestoregstate(current_context);  } 

thanks in advance..

most of code quite ok; problem occurs in following lines:

cgpoint startpoint = cgpointmake(0, 0); cgpoint endpoint = cgpointmake(40, 40); cgcontextdrawlineargradient(current_context, gradient, startpoint, endpoint, 0); 

cgcontextdrawlineargradient expects start , end point (defining line, not 2 diagonal edges of square!).

the gradient drawn colored lines perpendicular controlling line. drawing starts line going through startpoint (perpendicular line between startpoint , endpoint) using start color (color @ location 0). next line drawn through point 'one pixel' closer endpoint, color calculated somewhere between start , end or next color (depending of number of color locations). line drawn through endpoint (again perpendicular...) using end color.

the advantage of using (controlling) line instead of square is, gradient can drawn in direction; horizontally, vertically, somewhere between, depending of direction of given line.

in example code, gradient should diagonally, line has angle of 45° x-axis :-).


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -