Drawing-line drawing related

Source: Internet
Author: User
Tags dashed line

Recently, the company's project had to use some chart effects, and would have wanted to find some third parties on GitHub. However, the style of the third party and the company's needs are different, the change is more troublesome. So I wrote one, and it was generalized and now shared on GitHub. A bit of their own development process.

1. As soon as the first hand, want to completely use quartz2d to draw, however quartz2d can only draw some basic graphics, and can not achieve the animation effect of the path drawing. Want to fully use Uibezierpath (Uibezierpath actually also use OC syntax to quartz2d encapsulation, use more convenient) to draw, feel overqualified, and eventually feel the use of quartz2d to draw basic lines and text description, The Uibezierpath curve draws the path animation process. Note: quartz2d is rendered using CPU, so frequent drawings (such as updating graphics in real time in the touch Move method) can lead to high CPU occupancy, increased energy consumption and performance stress.

When drawing with quartz2d, it is recommended to do it directly inside the DrawRect method, which provides a graphical drawing context that can be obtained

Cgcontextref context = Uigraphicsgetcurrentcontext ();

Move to the point

Cgcontextmovetopoint (context, start.x, START.Y);

Connect to point from current context

Cgcontextaddlinetopoint (context, end.x, END.Y);

/* Path Color */

[Color Setstroke];

/* Fill color for path closure area */

[Color Setfill];

Set the dash style for a segment

CGFloat ss[] = {0.5,2,1};

Explain the following method parameters: the first parameter is the context the second parameter represents the distance segment of the pixel starting to draw the dashed line, the third represents the dashed line of the drawing rule array, such as the above SS array, which indicates that the first paragraph is 0.5 pixels long, followed by a 2-pixel dash, followed by a 1-pixel solid line. The fourth parameter indicates that if the third argument is passed into the array, the plotted dashed line pattern is executed to the number of the first array. If passed in 2, indicates that the drawing rule is the first two elements in the array.

Cgcontextsetlinedash (context, 0, SS, 2);

Here's one thing to emphasize: Arrays in Cgcontextsetlinedash should use CGFloat (although using other types may not give an error, but may cause the program to crash), do not use a float or double type array, because in iphone5s and below, The model is a 32-bit operating system, the above 64 is the operating system, so if you use a float or double type, the iphone5s and below will cause the program to crash, as follows, the system definition of cgfloat.

#if defined (__lp64__) && __lp64__

# define Cgfloat_type Double

# define Cgfloat_is_double 1

# define Cgfloat_min dbl_min

# define Cgfloat_max Dbl_max

#else

# define Cgfloat_type Float

# define Cgfloat_is_double 0

# define Cgfloat_min flt_min

# define Cgfloat_max Flt_max

#endif

Drawing-line drawing related

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.