Quartz 2D drawing for iOS development (107)

Source: Internet
Author: User
1 Preface

When using quartz 2D drawing, we usually use the subclass of uiview. Think about the drawrect of this class: add the quartz function call to the method. This method is called every time you need to repaint the view.

2. Details

In quartz 2D, like other core graphics, plotting is performed in the graphic context. Generally, it is called only the context. When drawing, We need to retrieve the current context, use this context for various quartz graphics calls, and make this context responsible for rendering the image to the view.

The following code retrieves the current context:

Cgcontextref context = uigraphicsgetcurrentcontext ();

Both core graphics and OpenGL are c-based APIs.

After the graphic context is defined, the context can be passed to various core graphics drawing functions for drawing. For example, draw a line with a 4 pixel width:

// Create a straight line of the current path in 4 pixels, which can be regarded as the size of the brush until a different value set by the function is called again, and the width of all lines is 4

Cgcontextsetlinewidth (context, 4.0 );

// Set the paint brush to red (including the stroke color, that is, the outline color. The fill color is used to fill the shape)

Cgcontextsetstrokecolorwithcolor (context, [uicolor redcolor]. cgcolor );

// Move the endpoint of the current path to this location without drawing any images.

Cgcontextmovetopoint (context, 10.0, 10.0 );

// Draw a line to (20, 20)

Cgcontextaddlinetopoint (context, 20.0, 20.0 );

// Instruct quartz to use cgcontextstrokepath to draw a straight line.

Cgcontextstrokepath (context );

2.1 Coordinate System

// Move the endpoint of the current path to this location without drawing any images.

Cgcontextmovetopoint (context, 10.0, 10.0 );

// Draw a line to (20, 20)

Cgcontextaddlinetopoint (context, 20.0, 20.0 );

These floating point numbers represent locations in the core graphics coordinate system. It is represented by X and Y coordinates. We usually use (x, y) to represent it. The upper left corner of the context is (0, 0 ). Y increases when you move down, and X increases when you move to the right.

Finally, we draw a diagonal line (10, 10) to (20, 20.

To obtain a certain application cgpoint, if the object size is obtained, cgsize is used. Quartz also declares a data type named cgrect, which is used to define a rectangle in the coordinate system. Cgrect contains two elements: one is the cgpoint named origin, which determines the upper left corner of the rectangle, and the other is the cgsize named size, which determines the width and height of the rectangle.

2.2 colors

Uikit provides an objective-C Class: uicolor. You cannot directly use the uicolor object in the core graphics call. Because uicolor is the cgcolor package, it can be as we did in the following code snippets, use her cgcolor attribute to retrieve cgcolor references from the uicolor instance.

// Set the paint brush to red (including the stroke color, that is, the outline color. The fill color is used to fill the shape)

Cgcontextsetstrokecolorwithcolor (context, [uicolor redcolor]. cgcolor );

2.2.1 color theory of iOS devices

In modern computers, four elements (red, green, blue (RGB color) model and transparency) are usually used to represent colors. In quartz 2D, these are cgfloat types and can only be between 0.0 and 1.0.

A floating point value ranging from 0.0 to 1.0 is usually called a qualified floating point variable. The color value also has a transparency Alpha, and the color model is rgba.

2.2.2 convenient color Method

Uicolor provides many convenient methods. You can use return [uicolor colorwithred: 1.0f Green: 0.0f Blue: 0.0f ALPHA: 1.0f] to customize the color values;

2.3 draw images in context

With quartz 2D, you can draw images directly in the context. For example, uiimage, method:

(1) create a cgpoint to determine the upper left corner of the image;

(2) create a cgrect to enclose the image and adjust the image size as needed to make it an appropriate overview.

You can draw a uiimage In the context as follows:

Cgpoint drawpoint = cgpointmake (100366f, 100366f );

[Image drawatpoint: drawpoint];

2.4 draw shape: polygon, straight line and curve

Quartz 2D provides many functions to simplify the creation of complex shapes. For example, draw an ellipse, define the rectangle it fits, and let core graphics execute the following tasks:

// Draw an ellipse

// Retrieve the current context

Cgcontextref context = uigraphicsgetcurrentcontext ();

Cgrect therect = cgrectmake (0, 0, 100,100 );

Cgcontextaddellipseinrect (context, therect );

Cgcontextdrawpath (context, kcgpathfillstroke );

Running result:

Line:

Elliptic:

3 conclusion

The above is all content and I hope it will help you.

DEMO code instance: http://download.csdn.net/detail/u010013695/5548373

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.