Examples of the basic use of quartz2d in IOS development _ios

Source: Internet
Author: User
Tags reserved

First, draw a straight line

Code:

Copy Code code as follows:

//
yylineview.m
03-Draw a straight line
//
Created by Apple on 14-6-9.
Copyright (c) 2014 itcase. All rights reserved.
//

#import "YYlineview.h"

@implementation Yylineview


The DrawRect method is invoked when the custom view is first displayed
-(void) DrawRect: (cgrect) rect
{

1. Gets the graphics context associated with the current view (because the graphics context determines which output target to draw)/
If the Uigraphicsgetcurrentcontext method is invoked in the DrawRect method, the layer context is obtained.
Cgcontextref Ctx=uigraphicsgetcurrentcontext ()//No need *, same ID

2. Drawing (drawing line), saving drawing information
Set Start point
Cgcontextmovetopoint (CTX, 20, 100);
Set End point
Cgcontextaddlinetopoint (CTX, 300, 100);


Set the state of a drawing
Set the color of the line to blue
Cgcontextsetrgbstrokecolor (CTX, 0, 1.0, 0, 1.0);
Set the width of a line
Cgcontextsetlinewidth (CTX, 15);
To set the start and end of a line with rounded corners
Cgcontextsetlinecap (CTX, Kcglinecapround);
To set the corner style of a line to rounded corners
Cgcontextsetlinejoin (CTX, Kcglinejoinround);
3. Render (draw out a hollow line)
Cgcontextstrokepath (CTX);

Note that the line cannot be rendered as solid
Cgcontextfillpath (CTX);



Set second line
Set the starting point for the second line
Cgcontextmovetopoint (CTX, 50, 200);
Sets the end of the second antenna (automatically the end of the previous line as the starting point)
Cgcontextaddlinetopoint (CTX, 300, 60);

Set the state of a drawing
Cgcontextsetrgbstrokecolor (CTX, 1.0, 0.7, 0.3, 1.0);
The second way to set the color
[[Uicolor Graycolor] set];
Set the width of a line
Cgcontextsetlinewidth (CTX, 10);
Set the style for the start and end of a line
Cgcontextsetlinecap (CTX, Kcglinecapbutt);

Render the second line of graphics to view
Draw a hollow line
Cgcontextstrokepath (CTX);
}


@end


Effect:

Second, draw the triangle

Code:

Copy Code code as follows:

//
yyrectview.m
02-Draw The Triangle
//
Created by Hole medical self on 14-6-10.
Copyright (c) 2014 itcast. All rights reserved.
//

#import "YYrectview.h"

@implementation Yyrectview


-(void) DrawRect: (cgrect) rect
{
1. Get Graphics context
Cgcontextref Ctx=uigraphicsgetcurrentcontext ();

2. Draw Triangles
Set Start point
Cgcontextmovetopoint (CTX, 20, 100);
Set a second point
Cgcontextaddlinetopoint (CTX, 40, 300);
Set a third point
Cgcontextaddlinetopoint (CTX, 200, 200);
Set End point
Cgcontextaddlinetopoint (CTX, 20, 100);
Close start and end points
Cgcontextclosepath (CTX);

3. Render graphics to layer
Cgcontextstrokepath (CTX);

}


@end


Effect:

Tip: Close start and end Cgcontextclosepath (CTX);

Three, draw quadrilateral

Code:

Copy Code code as follows:

//
Yyrect.m
03-Draw Quadrilateral
//
Created by Hole medical self on 14-6-10.
Copyright (c) 2014 itcast. All rights reserved.
//

#import "YYrect.h"

@implementation Yyrect


-(void) DrawRect: (cgrect) rect
{

1. Get the graphics context
Cgcontextref Ctx=uigraphicsgetcurrentcontext ();
2. Draw Quadrilateral
Cgcontextaddrect (CTX, CGRectMake (20, 20, 150, 100));

If you want to set the state of the drawing must be before rendering
Cgcontextsetrgbstrokecolor (CTX, 1.0, 0, 0, 1.0);
What type of graphic to draw (hollow or solid). What type of method to set the state
Cgcontextsetrgbfillcolor (CTX, 1.0, 0, 0, 1.0);

Invoke the OC method to set the color of the drawing
[[Uicolor Purplecolor] setfill];
[[Uicolor Bluecolor] setstroke];
Invoke the OC method to set the drawing color (both solid and hollow)
[[Uicolor Greencolor] set];
[[Uicolor colorwithred:1.0 green:0 blue:0 alpha:1.0] set];


3. Render graphics to layer
Hollow of
Cgcontextstrokepath (CTX);
Solid.
Cgcontextfillpath (CTX);

}


@end


Tip: If you want to set the state of the drawing, it must precede rendering.

Effects (solid and hollow):

Four, draw a circle

Code 1:

Copy Code code as follows:

-(void) DrawRect: (cgrect) rect
{

1. Getting context
Cgcontextref CTX = Uigraphicsgetcurrentcontext ();
Draw a Circle
Cgcontextaddarc (CTX, 0, 2 * m_pi, 0);

3. Render (Note that the drawing line can only be drawn through the hollow)
Cgcontextfillpath (CTX);
Cgcontextstrokepath (CTX);

}


Effect:

Code 2:

Copy Code code as follows:

Draw a Circle
1. Getting context
Cgcontextref CTX = Uigraphicsgetcurrentcontext ();
2. Draw a Circle
Cgcontextaddellipseinrect (CTX, CGRectMake (50, 100, 50, 50));

[[Uicolor Greencolor] set];

3. Rendering
Cgcontextstrokepath (CTX);
Cgcontextfillpath (CTX);

Effect:

Code 3:

Copy Code code as follows:

Draw an Ellipse
1. Getting context
Cgcontextref CTX = Uigraphicsgetcurrentcontext ();
2. Draw a Circle
Cgcontextaddellipseinrect (CTX, CGRectMake (50, 100, 100, 230));

[[Uicolor Purplecolor] set];

3. Rendering
Cgcontextstrokepath (CTX);
Cgcontextfillpath (CTX);

Effect:

V. Draw a circular arc

Code 1:

Copy Code code as follows:

Draw an arc
1. Getting context
Cgcontextref CTX = Uigraphicsgetcurrentcontext ();
2. Draw the Arc
X/y Center
Radius radius
StartAngle the beginning of the arc
Endangle the end of the arc
Clockwise the direction of the arc (0 clockwise, 1 counterclockwise)
Cgcontextaddarc (CTX, MB,-m_pi_2, m_pi_2, 0);
Cgcontextaddarc (CTX, MB, m_pi_2, M_PI, 0);
Cgcontextclosepath (CTX);

3. Rendering
Cgcontextstrokepath (CTX);
Cgcontextfillpath (CTX);

Effect:

Code 2:

Copy code code as follows:

1. Getting context
Cgcontextref CTX = Uigraphicsgetcurrentcontext ();
2. Gao-Shaped Map
Draw Line
Cgcontextmovetopoint (CTX, 100, 100);
Cgcontextaddlinetopoint (CTX, 100, 150);
Draw an arc
Cgcontextaddarc (CTX, MB, m_pi_2, M_PI, 0);
Cgcontextaddarc (CTX, MB,-M_PI, m_pi_2, 1);

Close path
Cgcontextclosepath (CTX);
[[Uicolor Browncolor]set];


3. Render (Note that the drawing line can only be drawn through the hollow)
Cgcontextfillpath (CTX);
Cgcontextstrokepath (CTX);

Effect:

Six, draw the text
Code:

Copy Code code as follows:

//
yytextview.m
04-Write text
//
Created by Hole medical self on 14-6-10.
Copyright (c) 2014 itcast. All rights reserved.
//

#import "YYtextview.h"

@implementation Yytextview


-(void) DrawRect: (cgrect) rect
{

Draw text
NSString *str = @ "The amount of search wind broke up the pink of the two sides said that OFFFF tile-roofed you f reply F into the membership dues WFH, fly, FN return to wfh; Oh delivery; F reply; Fhisfhsifh My skin good Apifrhi dividend Awfhiof Wei Feng network i";

1. Getting context
Cgcontextref CTX = Uigraphicsgetcurrentcontext ();
2. Drawing
It is not recommended to use the C language method to draw text, because the coordinate system in the quraz2d is inconsistent with the coordinate system in the Uikit, the text drawn out is reversed, and it is rather troublesome to draw text by means of the C language.
Cgcontextselectfont (< #CGContextRef C#>, < #const char *name#>, < #CGFloat size#> < #CGTextEncodi ng textencoding#>)
Cgcontextshowtext (CTX, < #const char *string#>, < #size_t length#>)

Draw a rectangle
1. Getting context
Cgcontextref CTX = Uigraphicsgetcurrentcontext ();
2. Drawing
Cgcontextaddrect (CTX, CGRectMake (50, 50, 100, 100));
3. Rendering
Cgcontextstrokepath (CTX);


Nsmutabledictionary *MD = [Nsmutabledictionary dictionary];
Set Text color
Md[nsforegroundcolorattributename] =[uicolor Redcolor];
Set Text background color
Md[nsbackgroundcolorattributename] = [Uicolor greencolor];
Set Text Size
Md[nsfontattributename] = [Uifont systemfontofsize:20];

Draw the text to the pointing position
[Str drawatpoint:cgpointmake (a) WITHATTRIBUTES:MD];

Draws the text to the specified range, if the line is not fit will be wrapped automatically, when the text is out of range does not appear
[Str drawinrect:cgrectmake (M, m, MB) Withattributes:nil];
}


@end


Effect:

Image

Code 1:

Copy Code code as follows:

//
Yyimage.m
04-Write text
//
Created by Hole medical self on 14-6-10.
Copyright (c) 2014 itcast. All rights reserved.
//

#import "YYimage.h"

@implementation Yyimage


-(void) DrawRect: (cgrect) rect
{

1. Load pictures into memory
UIImage *image = [UIImage imagenamed:@ "Me"];


Using the Drawaspatterninrec method to draw pictures to layer is by tiling the original picture
[Image drawaspatterninrect:cgrectmake (0, 0, 320, 480)];
}


@end


Effect (tile):

Code 2:

Copy Code code as follows:

#import "YYimage.h"

@implementation Yyimage


-(void) DrawRect: (cgrect) rect
{

1. Load pictures into memory
UIImage *image = [UIImage imagenamed:@ "Me"];


Using OC method to draw the picture to the layer

Using the Drawinrect method to draw pictures to layer is by stretching the original picture
[Image drawinrect:cgrectmake (0, 0, 200, 200)];

Using the Drawaspatterninrec method to draw pictures to layer is by tiling the original picture
[Image drawaspatterninrect:cgrectmake (0, 0, 320, 480)];
}


@end


Effects (stretching pictures):

Code 3:

Copy Code code as follows:

//
Yyimage.m
04-Write text
//
Created by Hole medical self on 14-6-10.
Copyright (c) 2014 itcast. All rights reserved.
//

#import "YYimage.h"

@implementation Yyimage


-(void) DrawRect: (cgrect) rect
{

1. Load pictures into memory
UIImage *image = [UIImage imagenamed:@ "Me"];


Using OC method to draw the picture to the layer

Draws a picture to a specified location
[Image Drawatpoint:cgpointmake (100, 100)];
}


Effect (draw the picture to a fixed position):

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.