Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What problems will be encountered when using Quartz2D

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "what problems you will encounter when using Quartz2D", the content is simple and clear, and I hope it can help you solve your doubts. Let the editor lead you to study and learn this article "what problems you will encounter when using Quartz2D".

Here are some of the problems that may be encountered in drawing:

Problem 1: when drawing multiple lines at the same time, after setting properties on the first line, all subsequent lines will be drawn with these attributes.

In fact, the solution is very simple, which is to reset the properties you draw when you draw a new line. For example, the property settings for the first line are:

CGContextSetLineWidth (ctx,12); / / Line width

[[UIColor redColor] set]; / / Line color

CGContextSetLineCap (ctx,kCGLineCapRound); / / end style

If you do not want to extend this property to continue drawing when you draw the second line, you can reset these properties:

CGContextSetLineWidth (ctx,1)

[[UIColor blackColor] set]

CGContextSetLineCap (ctx,kCGLineCapButt)

But this gives rise to a new problem. If you draw too many lines, you have to reset the properties every time you come to a new line, so it will be very troublesome to operate. Of course, it is impossible for Apple not to think of such a problem, and naturally there is a corresponding solution. But before we do that, we need to understand a new concept-the graphics context stack: the state used to save the graphics context.

The general process is: before setting the drawing properties of the first line, we first save the purest graphic context (at this time is the graphic context that has not done any operation), and then set the first line, before the second line, we take out the previously saved graphics context to draw, so that the second line drawing attributes are actually the default attributes of the system.

After getting the graphic context, call the function CGContextSaveGState (ctx)

Call the function CGContextRestoreGState (ctx) in front of the line where the property needs to be reset; you can

Two very simple parts, but to be clear, each time it is saved is one copy, and it will be gone after one call. If you want to reset it several times, save it several times, and then call it. If the number of calls is greater than the save times, you will hang up directly.

Question 2: how to make the rectangle rotate when drawing a rectangle?

In this case, we may encounter in the development, the solution to this problem is to use the function that comes with the system.

1 、. Get the context, and then

2. CGContextRotateCTM (ctx,M_Pi_4)

3 、.... Set paint attribut

4 、.... Render

The above method is used for the property setting of the rotation angle, it should be noted that the rotation here is not to rotate the drawing, but to rotate the entire layer, and all drawing is carried out on layer, so this function should be called before the drawing property is set, otherwise it will be invalid.

In addition to rotation, there are zooming and panning.

CGContextScaleCTM (ctx,x,y); / / scale (x represents a multiple of width and y represents a multiple of height)

CGContextTranslateCTM (ctx,x,y); / / Translation (x and y represent offsets on x and y, respectively)

Question 3: how to make the drawn picture appear as a circle?

The way to solve this problem: draw a circle first, draw the picture inside the circle, and the parts beyond the circle range are not displayed. Specific code:

1. Get the context.

2. CGContextAddEllipseInRect (ctx,CGRectMake (100, 100, 50, 50); / / draw a circle with a center (100100) and a radius of 50.

/ / A critical step

3. CGContextClip (ctx); / / specify the scope of the graph drawn above that can be displayed in the context

4. Just draw the picture to the point (100100).

Of course, in formal projects, pictures can be displayed in rectangles, triangles or other shapes according to different requirements.

The above is all the contents of this article entitled "what are the problems in using Quartz2D?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report