Summary of the progressive IOS color

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.