"Go" IOS Quartz various drawing graphics usage-achieve picture, write text, draw line, ellipse, rectangle, prism, etc.

Source: Internet
Author: User
Tags dashed line

Only override Drawrect:if perform custom drawing.
An empty implementation adversely affects performance during animation.
-(void) DrawRect: (cgrect) rect
{
Cgcontextref context = Uigraphicsgetcurrentcontext ();



/*no.1 Draw a line

Cgcontextsetrgbstrokecolor (context, 0.5, 0.5, 0.5, 0.5);//Line Color
Cgcontextmovetopoint (Context, 20, 20);
Cgcontextaddlinetopoint (context, 200,20);
Cgcontextstrokepath (context);
*/



/*no.2 Write text

Cgcontextsetlinewidth (context, 1.0);
Cgcontextsetrgbfillcolor (context, 0.5, 0.5, 0.5, 0.5);
Uifont *font = [Uifont boldsystemfontofsize:18.0];
[@] Company: BEIJING China Soft Technology Co., LTD. \ n Department: ERP business unit \ n Name: Mcliang "Drawinrect:cgrectmake ((), 280, Withfont:font];
*/


/*no.3 draw a square shape without a border

Cgcontextsetrgbfillcolor (context, 0, 0.25, 0, 0.5);
Cgcontextfillrect (Context, CGRectMake (2, 2, 270, 270));
Cgcontextstrokepath (context);
*/


/*no.4 draw a square border

Cgcontextsetrgbstrokecolor (context, 0.5, 0.5, 0.5, 0.5);//Line Color
Cgcontextsetlinewidth (context, 2.0);
Cgcontextaddrect (Context, CGRectMake (2, 2, 270, 270));
Cgcontextstrokepath (context);
*/


/*no.5 draw a square background color

CGCONTEXTTRANSLATECTM (context, 0.0f, self.bounds.size.height);
CGCONTEXTSCALECTM (context, 1.0f, -1.0f);
Uigraphicspushcontext (context);
Cgcontextsetlinewidth (context,320);
Cgcontextsetrgbstrokecolor (context, 250.0/255, 250.0/255, 210.0/255, 1.0);
Cgcontextstrokerect (context, CGRectMake (0, 0, 320, 460));
Uigraphicspopcontext ();
*/

/*no.6 Ellipse

CGRect arect= CGRectMake (80, 80, 160, 100);
Cgcontextsetrgbstrokecolor (context, 0.6, 0.9, 0, 1.0);
Cgcontextsetlinewidth (context, 3.0);
Cgcontextaddellipseinrect (context, arect); Elliptic
Cgcontextdrawpath (context, kcgpathstroke);
*/

/*no.7
Cgcontextbeginpath (context);
Cgcontextsetrgbstrokecolor (context, 0, 0, 1, 1);
Cgcontextmovetopoint (context, 100, 100);
Cgcontextaddarctopoint (Context, 50, 100, 50, 150, 50);
Cgcontextstrokepath (context);
*/

/*no.8 Gradient
Cgcontextclip (context);
Cgcolorspaceref RGB = Cgcolorspacecreatedevicergb ();
CGFloat colors[] =
{
204.0/255.0, 224.0/255.0, 244.0/255.0, 1.00,
29.0/255.0, 156.0/255.0, 215.0/255.0, 1.00,
0.0/255.0, 50.0/255.0, 126.0/255.0, 1.00,
};
Cggradientref gradient = cggradientcreatewithcolorcomponents
(RGB, colors, NULL, sizeof (colors)/(sizeof (COLORS[0)));
Cgcolorspacerelease (RGB);
Cgcontextdrawlineargradient (Context, Gradient,cgpointmake
(0.0,0.0), Cgpointmake (0.0,self.frame.size.height),
Kcggradientdrawsbeforestartlocation);
*/


/* No.9 four lines draw a square
Draw Line
Uicolor *acolor = [Uicolor colorwithred:0 green:1.0 blue:0 alpha:0];
Cgcontextsetrgbstrokecolor (context, 1.0, 0, 0, 1.0);
Cgcontextsetfillcolorwithcolor (context, acolor.cgcolor);
Cgcontextsetlinewidth (context, 4.0);
Cgpoint Apoints[5];
Apoints[0] =cgpointmake (60, 60);
APOINTS[1] =cgpointmake (260, 60);
APOINTS[2] =cgpointmake (260, 300);
APOINTS[3] =cgpointmake (60, 300);
APOINTS[4] =cgpointmake (60, 60);
Cgcontextaddlines (Context, apoints, 5);
Cgcontextdrawpath (context, kcgpathstroke); Start drawing lines
*/



/* No.10
Uicolor *acolor = [Uicolor colorwithred:0 green:1.0 blue:0 alpha:0];
Cgcontextsetrgbstrokecolor (context, 1.0, 0, 0, 1.0);
Cgcontextsetfillcolorwithcolor (context, acolor.cgcolor);
Elliptic
CGRect arect= CGRectMake (80, 80, 160, 100);
Cgcontextsetrgbstrokecolor (context, 0.6, 0.9, 0, 1.0);
Cgcontextsetlinewidth (context, 3.0);
Cgcontextsetfillcolorwithcolor (context, acolor.cgcolor);
Cgcontextaddrect (context, rect); Rectangular
Cgcontextaddellipseinrect (context, arect); Elliptic
Cgcontextdrawpath (context, kcgpathstroke);
*/



/* No.11
Draw a solid circle

Cgcontextfillellipseinrect (Context, CGRectMake (95, 95, 100.0, 100));
*/



/*no.12
Draw a Diamond
Cgcontextsetlinewidth (context, 2.0);
Cgcontextsetstrokecolorwithcolor (context, [Uicolor Bluecolor]. Cgcolor);
Cgcontextmovetopoint (context, 100, 100);
Cgcontextaddlinetopoint (context, 150, 150);
Cgcontextaddlinetopoint (context, 100, 200);
Cgcontextaddlinetopoint (context, 50, 150);
Cgcontextaddlinetopoint (context, 100, 100);
Cgcontextstrokepath (context);
*/

/*no.13 Draw a rectangle
Cgcontextsetlinewidth (context, 2.0);

Cgcontextsetstrokecolorwithcolor (context, [Uicolor Bluecolor]. Cgcolor);

CGRect rectangle = CGRectMake (60,170,200,80);

Cgcontextaddrect (context, rectangle);

Cgcontextstrokepath (context);
*/


/* Ellipse
Cgcontextsetlinewidth (context, 2.0);

Cgcontextsetstrokecolorwithcolor (context, [Uicolor Bluecolor]. Cgcolor);

CGRect rectangle = CGRectMake (60,170,200,80);

Cgcontextaddellipseinrect (context, rectangle);

Cgcontextstrokepath (context);
*/

/* Fills a path in red:

Cgcontextmovetopoint (context, 100, 100);
Cgcontextaddlinetopoint (context, 150, 150);
Cgcontextaddlinetopoint (context, 100, 200);
Cgcontextaddlinetopoint (context, 50, 150);
Cgcontextaddlinetopoint (context, 100, 100);
Cgcontextsetfillcolorwithcolor (context, [Uicolor Redcolor]. Cgcolor);
Cgcontextfillpath (context);
*/

/* Fill a red rectangle with a blue edge
Cgcontextsetlinewidth (context, 2.0);
Cgcontextsetstrokecolorwithcolor (context, [Uicolor Bluecolor]. Cgcolor);
CGRect rectangle = CGRectMake (60,170,200,80);
Cgcontextaddrect (context, rectangle);
Cgcontextstrokepath (context);
Cgcontextsetfillcolorwithcolor (context, [Uicolor Redcolor]. Cgcolor);
Cgcontextfillrect (context, rectangle);
*/

/* Arc Drawing
The arc is drawn by specifying two tangency points, as well as an angle, called cgcontextaddarctopoint ()
Cgcontextsetlinewidth (context, 2.0);
Cgcontextsetstrokecolorwithcolor (context, [Uicolor Bluecolor]. Cgcolor);
Cgcontextmovetopoint (context, 100, 100);
Cgcontextaddarctopoint (context, 100,200, 300,200, 100);
Cgcontextstrokepath (context);
*/


/*
Draw a Bézier curve
The Bezier curve is drawn by moving a starting point, then through two control points, and a stop point, called the Cgcontextaddcurvetopoint () function.
Cgcontextsetlinewidth (context, 2.0);

Cgcontextsetstrokecolorwithcolor (context, [Uicolor Bluecolor]. Cgcolor);

Cgcontextmovetopoint (context, 10, 10);

Cgcontextaddcurvetopoint (context, 0, 50, 300, 250, 300, 400);

Cgcontextstrokepath (context);
*/

/* Draw two Bézier curves

Cgcontextsetlinewidth (context, 2.0);

Cgcontextsetstrokecolorwithcolor (context, [Uicolor Bluecolor]. Cgcolor);

Cgcontextmovetopoint (context, 10, 200);

Cgcontextaddquadcurvetopoint (context, 150, 10, 300, 200);

Cgcontextstrokepath (context);
*/

/* Draw Dashed lines
Cgcontextsetlinewidth (context, 5.0);

Cgcontextsetstrokecolorwithcolor (context, [Uicolor Bluecolor]. Cgcolor);

CGFloat dasharray[] = {2,6,4,2};

Cgcontextsetlinedash (Context, 3, Dasharray, 4);//Skip 3 and draw a dashed line, so just start with 6-(3-2) = 5 Virtual dots

Cgcontextmovetopoint (context, 10, 200);

Cgcontextaddquadcurvetopoint (context, 150, 10, 300, 200);

Cgcontextstrokepath (context);
*/
/* Draw a picture
nsstring* ImagePath = [[NSBundle mainbundle] pathforresource:@ "dog" oftype:@ "png"];
uiimage* myimageobj = [[UIImage alloc] initwithcontentsoffile:imagepath];
[Myimageobj drawatpoint:cgpointmake (0, 0)];
[Myimageobj drawinrect:cgrectmake (0, 0, 320, 480)];

NSString *s = @ "My puppy";

[s Drawatpoint:cgpointmake (0) Withfont:[uifont systemfontofsize:34.0]];
*/

/*
NSString *path = [[NSBundle mainbundle] pathforresource:@ "dog" oftype:@ "png"];
UIImage *img = [UIImage Imagewithcontentsoffile:path];
CGIMAGEREF image = img. Cgimage;
Cgcontextsavegstate (context);
CGRect touchrect = CGRectMake (0, 0, img.size.width, img.size.height);
Cgcontextdrawimage (context, touchrect, image);
Cgcontextrestoregstate (context);
*/


/*nsstring *path = [[NSBundle mainbundle] pathforresource:@ "dog" oftype:@ "png"];
UIImage *img = [UIImage Imagewithcontentsoffile:path];
CGIMAGEREF image = img. Cgimage;
Cgcontextsavegstate (context);

CGCONTEXTROTATECTM (context, M_PI);
CGCONTEXTTRANSLATECTM (context,-img.size.width,-img.size.height);

CGRect touchrect = CGRectMake (0, 0, img.size.width, img.size.height);
Cgcontextdrawimage (context, touchrect, image);
Cgcontextrestoregstate (context); */

/*
NSString *path = [[NSBundle mainbundle] pathforresource:@ "dog" oftype:@ "png"];
UIImage *img = [UIImage Imagewithcontentsoffile:path];
CGIMAGEREF image = img. Cgimage;

Cgcontextsavegstate (context);

Cgaffinetransform myaffine = cgaffinetransformmakerotation (M_PI);
Myaffine = Cgaffinetransformtranslate (Myaffine,-img.size.width,-img.size.height);
CGCONTEXTCONCATCTM (context, myaffine);

CGCONTEXTROTATECTM (context, M_PI);
CGCONTEXTTRANSLATECTM (context,-img.size.width,-img.size.height);

CGRect touchrect = CGRectMake (0, 0, img.size.width, img.size.height);
Cgcontextdrawimage (context, touchrect, image);
Cgcontextrestoregstate (context);
*/
}

"Go" IOS Quartz various drawing graphics usage-achieve picture, write text, draw line, ellipse, rectangle, prism, etc.

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.