M beef C Original Blog--ios in the quartz2d use of attention to knowledge points

Source: Internet
Author: User

1 > Quartz2d Introduction

What is quartz2d? two-dimensional drawing engine

What is two-dimensional ? plane

What is an engine ? a library of packaged functions for developers to use. Which means that Apple helped us encapsulate a set of drawing libraries .

What does it mean to support both IOS and Mac systems ? with quartz2d write the same code that can run both in iphone and can be run on the mac , you can develop across platforms.

A screenshot / crop / custom UI control is more commonly used in development .

quartz2d in the IOS the value in development is custom UI control.

The data type and function of the graphics context.

How many of these are planted below.

The steps for customizing the control.

Why implement the DrawRect: method, because the context is only available in the drawrect: method


2 > quartz2d Drawing Walkthroughs

Where's the painting ? Storyboard Drag a viewto draw somethingin this view .

Custom view: You must rewrite the drawrect if you need to draw : method

* Hmlineview: Draw Segment must draw analysis

DrawRect: how the method is automatically generated and what it means ? This method is very important.

1> when to call : When the view is to be displayed , it is called only after the viewdidload is called, Because the view was not displayed at that time.

2> function : used for drawing

    * Draw a line

1> Get the graphics context

CG: indicates that this class is Coregraphics in the frame Ref: References

The current learning context is related to uigraphics , want to get the graphics context, first knock uigraphics.

2> Stitching Path: In general development with a Bezier path, which encapsulates a lot of things, you can help me draw some basic lines, rectangles, circles and so on.

Create a Bezier path

starting point : moveToPoint

end point : Addlinetopoint

3> Add a path to the context

Cgpath Conversion: UIKit Frame Turn Coregraphics directly Cgpath you can turn .

4> render the context to the view, the graphics context itself does not have a display function.

PPT Drawing Analysis Why do you do this ? ? first get the graphics context, then describe the path, add the path to the context, render to the view, the graphics context is equivalent to a memory buffer, in memory operation is the fastest, much faster than directly in the interface operation.


    * when you add a line

Direct addlinetopoint, because the path is spliced, the default is the beginning of the next line is the end of the previous line.


    * draw two lines that are not connected

1> Second Draw, reset the starting point, and then draw the line. A path can contain more than one segment.

2> Create a new path to add to the context. This is recommended in development, and it is easier to control each line.


    * Set Drawing status

How to add a thick line.

Drawing status Call order: As long as it is good before rendering, you will see the final state of the drawing when rendering.


    * Draw a curve

PPT Analysis: 3 Point, Start point, end point, control point.


* Hmshapeview: Draw a graphic

* Triangle triangles (drawing analysis)

    * Close Path Closepath : Connect to the starting point from the end of the path

    * Fill Path Cgcontextfillpath : It can be filled with a closed path.

    * Set Fill Color [[Uicolor Bluecolor] setfill];

    * Set Stroke color [[Uicolor Redcolor] setstroke];

    * does not show the stroke color, why ? No line width set

    * set the LineWeight, or do not display, why ? because the drawing path is not correct.

    * that is, fill and stroke Cgcontextdrawpath:kcgpathfillstroke .


* Rectangle Rectangle


* Circle Circle: Why the rectangle is passed in because the circle is tangent to the rectangle


* Arc Arc

PPT Analysis :

1> arcs are part of a circle, so you have to have a circle first to have an arc.

2> does the circle need a starting point? Draw line need, circle also not additional.

3> where is the starting point ? Center Right

4> Draw arc also need to start angle, end angle, direction, angle must be radian



* Quarter 1 / 4 round

      * Draw a 1 / 4 arcs, Stroke

      * fill the path, Fill

      * must have a closed path to fill, no closed path, the system will automatically close the path, and then to fill

      * draws the line to connect the center, automatically closes the path.



2.1 Basic graphic Drawing :

First draw PPT in writing code

instruction sequence : draw line: Draw line , add a line - Draw two lines ( path fill, set path properties, color, lineweight, etc. ) Curve - Analysis

Instruction Order : drawing a shape: drawing a triangle ( superman underwear ) circular arc , rectangle ,1/ 4 Yuan


3 > Redraw - Download progress bar (Setneedsdisplay) must draw analysis

    * there are several controls for parsing: Uisliderview, Custom View used to draw . , UILabel

    * Analysis ideas: Sliding Uisliderview the time, put Uisliderview the value passed to the custom View, Change the display.

    * How to monitor Uisliderview , ValueChange Events

    * Custom View Get a property to receive sliding values

    * let 's do it now. label, because he is relatively simple, to do things, first from a simple start.

    * Lazy Loading label, center position, text centered

* Label How to display ? rewrite Progress of the Set Method , data is displayed in label on.

    * draw arcs, PPT analysis, starting from where to draw arcs.

    * How many times per turn ° ? angle = progress * M_PI * 2

* StartAngle =-m_pi_2

* Endangle =-m_pi_2 + Progress * M_PI * 2

    * does not update the view display in time ? why, because DrawRect It is only called once when the view is displayed.

    * How to redraw?

    * Call Manually DrawRect method, no, because you cannot create the context yourself, you must call the system to invoke the

* Setneedsdisplay : In View To make a redraw mark, the next time the drawing cycle comes, the context is created, and then automatically called DrawRect Redraw.



4 > Draw a pie chart

* PPT Analysis - Find the Rules - come to Starta,enda,angle the conclusion

* Starta = enda angle = proportions * Total Degrees the °enda = starta + Angle

* angle = own / 100.0 * the

    * a picture of a fan

5 > Draw a bar chart

* PPT Analysis - Find the Rules - come to w,h,y,x the conclusion

* W = VIEWW/(2 * Count-1)

* H = viewh * proportions


6 > UIKit Encapsulating drawing Methods Walkthrough ( no context required )

6.0 Draw Text

6.1 Draw a rectangle

6.2 Drawing an image

6.4 image crop , after you specify a trimmed area, all the drawing information displays only the trim area


7 > Imitation uiimageview/ Snowflakes ( timer )


8 > Graphics Context Stack


9 > Matrix Transformations





M beef C Original Blog--ios in the quartz2d use of attention to knowledge points

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.