IOS quartz2d Gradient Graphics cggradient cgshading

Source: Internet
Author: User

Recently learning iOS quartz2d two-dimensional graphics drawing---> Gradient effects


quartz2d Gradient
Quartz provides two opaque data Odgago create gradients: Cgshadingref and Cggradientref
You can use either to create an axial (axial) or radial (radial) gradient. A gradient is a fill from one color to another
An axial gradient (also a linear gradient)





Do not say nonsense directly on the dry, code and comments are all, do not understand the self-Baidu bar

<span style= "FONT-SIZE:18PX;" >//quartz2dviewthree.m//quartz2ddemoone////Created by the Emperor Phlogistic Demon on 16/5/22.//copyright©2016 year. All rights reserved.//#import ' Quartz2DViewThree.h ' @implementation quartz2dviewthree//only override Drawrect:if Perform custom drawing.//an empty implementation adversely affects performance during animation.-(void) DrawRect: (CGRect        Rect {//Drawing code [Super Drawrect:rect];    Cgcontextref context = Uigraphicsgetcurrentcontext (); /** * quartz2d Gradient quartz provides two opaque data Odgago create gradients: Cgshadingref and cggradientref You can use either of these to create an axial (axial) or radial (radial) gradient. A gradient is filled from one color to another with an axial gradient (also becoming a linear gradient) *//[Self mygradientwithstartpoint:cgpointmake (self.bounds.size.width, 0)           Endpoint:cgpointmake (0, Self.bounds.size.height)];     Gradientcolor (context, self.bounds);    Mypaintradialshading (context, self.bounds); } #pragma mark-----cggradientcreatewithcolorcomponents Create a gradient layer-(void) Mygradientwithstartpoint: (cgpoint) StartPoint endPoint: (cgpoint) endpoint{cgcontextref context = Uigraphicsgetcurrentcontext ();    Cgpoint Mystartpoint, Myendpoint;    Mystartpoint.x = Startpoint.x;    Mystartpoint.y = Startpoint.y;    Myendpoint.x = Endpoint.x;        Myendpoint.y = Endpoint.y;    Cggradientref mygradient;    Cgcolorspaceref Mycolorspace;    size_t num_locations = 4; CGFloat Locations[4] = {0.0, 0.33, 0.66, 1.0}; Set 4 points cgfloat components[16] = {0.1, 1.0, 0.2, 1.0,//RGB Alpha 1.0, 0.1, 0.1, 1.0, 0.1, 0.1, 1.0, 1.    0, 1.0, 1.0, 1.0, 1.0};    Mycolorspace = Cgcolorspacecreatewithname (KCGCOLORSPACEGENERICRGB); /** * cggradient * * @param mycolorspace Set color space hue * @param components set key color array * @param loc Ations set the location and number of key points * @param num_locations number of Set points * * @return cggradient */mygradient = Cggradien            Tcreatewithcolorcomponents (Mycolorspace, components, locations, num_locations); CgcontextdrawrAdialgradient (context, mygradient, Mystartpoint, Myendpoint, kcggradientdrawsbeforestartlocation);        Cgcontextdrawlineargradient (context, mygradient, Mystartpoint, Myendpoint, kcggradientdrawsbeforestartlocation);        Releases the Color space object Cgcolorspacerelease (Mycolorspace); }/** * Using the Cgshading object to draw a radial gradient * * Use the Cgshading object to generate the following figure in order to draw a radial gradient, the following steps are required: 1. Set the Cgfunction object to calculate the color value---->>> to have a A method to calculate the color function 2 for this graph. Create a radial gradient of the Cgshading object----->>>> Create a Cgshading object to draw a gradient shape 3. Use the Cgshading object to draw a radial gradient 4, release the object-----> Don't forget to release the object cgshading object */#pragma mark----The effect of the gradient circle---> cgshading the case of the radial gradient is plotted void Mypai Ntradialshading (cgcontextref mycontext, cgrect bounds) {Cgpoint startPoint,//gradient starting point coordinates endPoint;//gradient end point coordinate C    Gfloat Startradius, Endradius;    Cgaffinetransform Mytransform;    CGFloat width = bounds.size.width;    CGFloat height = bounds.size.height; /** * The coordinates of the initial point and the end point coordinates are the system's coordinate systems standard by setting the starting and ending points of coordinates and radii you can draw various shapes by setting the color function you can get five YanSix-Color Graphics * * */StartPoint = Cgpointmake (. 4,.4);    Startradius = 1.;    EndPoint = Cgpointmake (. 5,.5);        Endradius =. 01;    CMYK mode in the state of the color will be darker a little cgcolorspaceref colorspace = Cgcolorspacecreatedevicergb ();            Create a Cgfunctionref object to calculate the color value cgfunctionref myshadingfunction = mygetfunction (colorspace); Create a cgshadingref to draw a radial gradient/** * cgshadingref * * @param colorspace Cgcolorspace Object color space * @pa Ram StartPoint The starting point coordinates for the axial gradient, with the starting and ending points of the axis, and for radial gradients, the coordinates of the start circle and the center of the ending circles * @param startradius the radius of the starting point circle---> used to define The starting radius of the circle of the gradient area * @param endPoint end point coordinates * @param the radius of the Endradius end point Circle----> The end radius of the circle used to define the gradient area * @  param myshadingfunction cgfunction function calculated for the color function * @param extendstart false----> whether to extend to the initial point * @param Extendend false---> whether to extend to the terminating point * * @return Returns a Cgshadingref object that is configured with a gradient color space, starting point, radius of the starting point circle, end point, Circle of the ending point RADIUS, the return function of the color, after getting this object, start drawing the gradient graphic */CgshadIngref shading = cgshadingcreateradial (ColorSpace, StartPoint, Startradius , EndPoint, Endradius, MyS        Hadingfunction, False, false);    The CTM Matrix Mytransform = Cgaffinetransformmakescale (width, height) that obtains the transformation of the graph;    CGCONTEXTCONCATCTM (Mycontext, mytransform);        Save the state of the graphic context cgcontextsavegstate (mycontext);    Trim clip a graphic cgcontextcliptorect (mycontext, cgrectmake (0, 0, 1, 1));    Sets the RGB color of the fill Cgcontextsetrgbfillcolor (Mycontext, 1, 1, 1, 1);        Set up a filled graph of frame cgcontextfillrect (mycontext, cgrectmake (0, 0, 1, 1));    Draw Shading Cgcontextdrawshading (mycontext, shading);    Note: Do not forget to release the color space ColorSpace, Shading Intermediate drawing object, the method of calculating color cgfunction cgcolorspacerelease (colorspace);    Release the Cgshading object Cgshadingrelease (shading); Release the calculated color is worth the function object Cgfunctionrelease (Myshadingfunction); Restore Status Restore Context cgcontextrestoregstate (mycontext);} /** * Set the Cgfunction object to calculate the color value * * Calculation of the radial and axial gradient color value functions is no different, we can calculate the color value according to the axial setting above the Cgfunction object, the function follows the same prototype, each function gets an input value and calculates n values, which are the color space for each color component plus an alpha value after the color calculation function is called, you need to create a Cgfunction object, such as setting the Cgfunction object in the axial direction to calculate the color value */static void Mycalculateshadi        Ngvalues (void *info, const cgfloat *in, CGFloat *out) {size_t k, components;    /** * Double value array controls the corresponding R,g,b,alpha equivalent of the color pair in the group */double frequency[4] = {220, 220, 220, 0};    components = (size_t) info;    for (k = 0; k < components-1; k++)//by a sin function to give the color space an RGB value *out++ = (1 + sin (*in * frequency[k]))/2; *out = 1;} /** * Mygetfunction Create myfunction to shading a color callback function * * */static cgfunctionref mygetfunction (cgcolorspaceref colorspace)    {//Create a cgfloat array, value static const cgfloat Input_value_range[2] = {0, 1};    static const CGFloat Output_value_ranges[8] = {0, 1, 0, 1, 0, 1, 0, 1}; static const cgfunctioncalLbacks callbacks = {0, &mycalculateshadingvalues, NULL};            size_t numcomponents = 1 + cgcolorspacegetnumberofcomponents (colorspace); /** * cgfunctioncreate function * 1. A pointer to the data required by the callback, 2. The number of input values for the callback, Quartz requires the callback to carry an input value *///Return a method to create Cgfunctioncreate cgfunction return cgfunctioncreate ((void *) NUMC                             Omponents, 1, Input_value_range, numcomponents, Output_value_ranges, &callbacks);} #pragma mark---the effect of cgshading axial gradient void Gradientcolor (Cgcontextref mycontext, cgrect bounds) {Cgpoint startPoint,    EndPoint;    Cgaffinetransform Mytransform;    CGFloat width = bounds.size.width;        CGFloat height = bounds.size.height;    StartPoint = Cgpointmake (0,0.5);        EndPoint = Cgpointmake (1,0.5);    Cgcolorspaceref colorspace = Cgcolorspacecreatedevicergb ();        Cgfunctionref myshadingfunction = mygetgradientfunction (colorspace); Cgshadingref shading = CGSHADINGCReateaxial (ColorSpace, StartPoint, EndPoint,        Myshadingfunction, False, false);    Mytransform = Cgaffinetransformmakescale (width, height);    CGCONTEXTCONCATCTM (Mycontext, mytransform);        Cgcontextsavegstate (Mycontext);    Cgcontextcliptorect (Mycontext, cgrectmake (0, 0, 1, 1));    Cgcontextsetrgbfillcolor (Mycontext, 1, 1, 1, 1);        Cgcontextfillrect (Mycontext, cgrectmake (0, 0, 1, 1));    Cgcontextbeginpath (Mycontext);    Cgcontextaddarc (Mycontext,. 5,. 5,. 3, 0, M_PI, 0);    Cgcontextclosepath (Mycontext);        Cgcontextclip (Mycontext);    Cgcontextdrawshading (mycontext, shading);    Cgcolorspacerelease (ColorSpace);    Cgshadingrelease (shading);        Cgfunctionrelease (myshadingfunction);    Cgcontextrestoregstate (Mycontext); }static cgfunctionref mygetgradientfunction (cgcolorspaceref colorspace) {static const cgfloat Input_Value_range[2] = {0, 1};    static const CGFloat Output_value_ranges[8] = {0, 1, 0, 1, 0, 1, 0, 1};    static Const Cgfunctioncallbacks callbacks = {0, &mygradientcalculateshadingvalues, NULL};    size_t numcomponents = 1 + cgcolorspacegetnumberofcomponents (colorspace);                            return cgfunctioncreate (void *) numcomponents, 1, Input_value_range, Numcomponents, Output_value_ranges, &callbacks);}    static void Mygradientcalculateshadingvalues (void *info, const cgfloat *in, CGFloat *out) {CGFloat v;    size_t k, components;    static const CGFloat C[] = {1, 0,. 5, 0};    components = (size_t) info;    v = *in;    for (k = 0; k < components-1; k++) *out++ = c[k] * v; *out = 1;} @end </span>


IOS quartz2d Gradient Graphics cggradient cgshading

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.