"iOS Dev-80" quartz2d Drawing Introduction: Line/Circle/ellipse/square and context stack management Cgcontextsavegstate/cgcontextrestoregstate

Source: Internet
Author: User




-(void) DrawRect: (cgrect) Rect {//Get current Context cgcontextref ctx=uigraphicsgetcurrentcontext ();        Save the current context state in the stack cgcontextsavegstate (CTX);    Zoom, move processing (need to be set before drawing) CGCONTEXTSCALECTM (CTX, 0.5, 0.5);    CGCONTEXTTRANSLATECTM (CTX, 100, 100);        CGCONTEXTROTATECTM (CTX, m_pi_4);    Stroke Cgcontextmovetopoint (CTX, 10, 10);    Cgcontextaddlinetopoint (CTX, 100, 100);    Cgcontextaddlinetopoint (CTX, 150, 50);    The following two methods can be closed-loop//cgcontextaddlinetopoint (CTX, 10, 10);    Cgcontextclosepath (CTX);    Render drawing, solid and hollow Cgcontextstrokepath (CTX);        Cgcontextfillpath (CTX);        Save the current context state in the stack cgcontextsavegstate (CTX);    Draw a square cgcontextaddrect (CTX, CGRectMake (100, 100, 50, 50)); Set the line width (be sure to cgcontextstrokepath before)//Because there was a previous rendering of the drawing, so this property setting does not affect the above triangle, the following color settings are the same//So, if you want to set the properties of two or more graphs, render the drawing once the CG    Contextsetlinewidth (CTX, 10);    Set the color (in the same vein, the code for the property setting is preceded by the drawing's code) Cgcontextsetrgbstrokecolor (CTX, 1, 0, 0, 1);        Cgcontextstrokepath (CTX); Set Style CgcontexTmovetopoint (CTX, 20, 160);    Cgcontextaddlinetopoint (CTX, 200, 280);    Cgcontextaddlinetopoint (CTX, 250, 200);    Cgcontextsetlinewidth (CTX, 20);    Set the Kinsoku style Cgcontextsetlinecap (CTX, Kcglinecapround);    Set the corner style Cgcontextsetlinejoin (CTX, Kcglinejoinround);        Cgcontextstrokepath (CTX); Remove the context state stored in the stack and restore it.        The above code setting style does not affect other cgcontextrestoregstate (CTX);    Draw Oval Cgcontextaddellipseinrect (CTX, CGRectMake (200, 130, 60, 30));    The following equivalent//cgcontextstrokepath (CTX);        Cgcontextdrawpath (CTX, Kcgpathstroke);    Draw round Cgcontextaddellipseinrect (CTX, CGRectMake (140, 170, 50, 50));    Cgcontextsetlinewidth (CTX, 3);        Cgcontextstrokepath (CTX);    Draw Arc Cgcontextaddarc (CTX, M_pi_4, M_PI, 1);        Cgcontextstrokepath (CTX);    Draw 1/4 circles, as well as set new methods of color Cgcontextmovetopoint (CTX, 10, 230);    Cgcontextaddlinetopoint (CTX, 10, 280);    Cgcontextaddlinetopoint (CTX, 60, 280);    Cgcontextaddarc (CTX, ten, 280, 0,-m_pi_2, 1); [[Uicolor Greencolor]Setstroke];        Cgcontextstrokepath (CTX);    Draw pictures and text (do not need to get the context manually) NSString *[email protected] "Xin-chou-an-year-empty work";    [str1 Drawatpoint:cgpointzero Withattributes:nil];    UIImage *img=[uiimage imagenamed:@ "001"];    [img Drawatpoint:cgpointmake (10, 10)];    Overlap The picture in a box and sign CGRect rect1=cgrectmake (50, 50, 100, 100);    [img Drawaspatterninrect:rect1];    Nsmutabledictionary *attr=[[nsmutabledictionary Alloc]init];    Attr[nsforegroundcolorattributename]=[uicolor Whitecolor];    Attr[nsfontattributename]=[uifont Systemfontofsize:13];        [Str1 Drawinrect:cgrectmake (+, withattributes:attr, +)]; Remove the context state stored in the stack and restore it.        The above code setting style does not affect other cgcontextrestoregstate (CTX);    Crop round head cgcontextaddellipseinrect (CTX, CGRectMake (150, 150, 100, 100));    Clipping a context area according to a circle, and the contents are then filled in the circular context cgcontextclip (CTX);    UIImage *img1=[uiimage imagenamed:@ "Me"]; [Img1 Drawatpoint:cgpointmake (150, 150)];}


"iOS Dev-80" quartz2d Drawing Introduction: Line/Circle/ellipse/square and context stack management Cgcontextsavegstate/cgcontextrestoregstate

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.