I have been working on iPhone custom controls recently. I have some knowledge about the graphic control and Some APIs on iOS, so I would like to summarize them.
Gradual color is the basic thing for making images. It is essential to make beautiful images.
The basic API used is cggradientref.
/**
The progressive color method for Drawing Images. This method only supports two-color gradient values.
@ Param context cgcontextref of the graphic Context
@ Param cliprect the rect to be colored
@ Param startpoint: coordinates of the starting point of the color.
@ Param endpoint: draws the coordinates of the end point of the color.
@ Param options cggradientdrawingoptions
@ Param startcolor
@ Param endcolor end color value
*/
-(Void) drawgradientcolor :( cgcontextref) Context
Rect :( cgrect) cliprect
Point :( cgpoint) startpoint
Point :( cgpoint) Endpoint
Options :( cggradientdrawingoptions) Options
Startcolor :( uicolor *) startcolor
Endcolor :( uicolor *) endcolor
{
Uicolor * Colors [2] = {startcolor, endcolor };
Cgcolorspaceref RGB = cgcolorspacecreatedevicergb ();
Cgfloat colorcomponents [8];
For (INT I = 0; I <2; I ++ ){
Uicolor * color = colors [I];
Cgcolorref temcolorref = color. cgcolor;
Const cgfloat * components = cgcolorgetcomponents (temcolorref );
For (Int J = 0; j <4; j ++ ){
Colorcomponents [I * 4 + J] = components [J];
}
}
Cggradientref gradient = cggradientcreatewithcolorcomponents (RGB, colorcomponents, null, 2 );
Cgcolorspacerelease (RGB );
Cgcontextdrawlineargradient (context, gradient, startpoint, endpoint, options );
Cggradientrelease (gradient );
}
This method implements a color gradient, but this is only a two-color gradient. If you want a multi-color gradient, the uicolor * array is generated to the number of responses, at the same time, when retrieving the color to generate cgcolorref, use the item subscript of the array length to extract the color one by one. At the same time, we need to use cliprect to draw a progressive color width here. This rect size needs to be defined, and we need to cut the context into the size of this rect before it is given.
For example, if our current context is for the entire screen, we need to take a rect in the middle, then we can hold the current context first.
Cgcontextsavegstate (context );
Then we extract the corresponding Context
Cgcontextcliptorect (context, cliprect );
......
......
After using this context, we need to restore it to the previous context
Cgcontextrestoregstate (context );
So far, it is complete. What I implement is to draw a rectangle in the screen and then implement the progressive color function in the rectangle. You can try it.