PathCreationandPathPainting

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

【Path Creation and Path Painting】

Path creation and path painting are separate tasks. First you create a path. When you want to render a path, you request Quartz to paint it. As you can see in Figure 3-1, you can choose to stroke the path, fill the path, or both stroke and fill the path. You can also use a path to constrain the drawing of other objects within the bounds of the path creating, in effect, aclipping area.

1、Point:CGContextMoveToPoint

2、Lines:CGContextAddLineToPoint、CGContextAddLines

3、Arcs:CGContextAddArc、CGContextAddArcToPoint

4、Curves:CGContextAddCurveToPoint、CGContextAddQuadCurveToPoint

To close the current subpath, yourapplicationshould callCGContextClosePath. This function adds a line segment from the current point to the starting point of the subpath and closes the subpath. Lines, arcs, and curves that end at the starting point of a subpath do not actually close the subpath. You must explicitly callCGContextClosePathto close a subpath.

Some Quartz functions treat a path’s subpaths as if they were closed by your application. Those commands treat each subpath as if your application had calledCGContextClosePathto close it, implicitly adding a line segment to the starting point of the subpath.

After closing a subpath, if your application makes additional calls to add lines, arcs, or curves to the path, Quartz begins a new subpath starting at the starting point of the subpath you just closed.

5、Ellipses:CGContextAddEllipseInRect

6、Rectangles:CGContextAddRect、CGContextAddRects

【Creating a Path】

A graphics context can haveonly a single pathin use at any time. If the specified context already contains a current path when you call this function, Quartz discards the old path and any data associated with it.

The current path is not part of the graphics state. Consequently,saving and restoring the graphics state has no effect on the current path.

When you want to construct a path in a graphics context, you signal Quartz by calling the functionCGContextBeginPath. Next, you set the starting point for the first shape, or subpath, in the path by calling the functionCGContextMoveToPoint. After you establish the first point, you can add lines, arcs, and curves to the path, keeping in mind the following:

Before you begin a new path, call the functionCGContextBeginPath.Lines, arcs, and curves are drawn starting at the current point. An empty path has no current point; you must callCGContextMoveToPointto set the starting point for the first subpath or call a convenience function that implicitly does this for you.When you want to close the current subpath within a path, call the functionCGContextClosePathto connect a segment to the starting point of the subpath. Subsequent path calls begin a new subpath, even if you do not explicitly set a new starting point.When you draw arcs, Quartz draws a line between the current point and the starting point of the arc.Quartz routines that add ellipses and rectangles add a new closed subpath to the path.You must call a painting function to fill or stroke the path because creating a path does not draw the pathAfter you paint a path, it is flushed from the graphics context. You might not want to lose your path so easily, especially if it depicts a complex scene you want to use over and over again. For that reason, QuartzPRovides two data types for creating reusable paths—CGPathRefandCGMutablePathRef. You can call the functionCGPathCreateMutableto create a mutable CGPath object to which you can add lines, arcs, curves, and rectangles. Quartz provides a set of CGPath functions that parallel the functions discussed in“The Building Blocks.”The path functionsOperate on a CGPath object instead of a graphics context. These functions are:

CGPathCreateMutable, which replacesCGContextBeginPathCGPathMoveToPoint, which replacesCGContextMoveToPointCGPathAddLineToPoint, which replacesCGContextAddLineToPointCGPathAddCurveToPoint, which replacesCGContextAddCurveToPointCGPathAddEllipseInRect, which replacesCGContextAddEllipseInRectCGPathAddArc, which replacesCGContextAddArcCGPathAddRect, which replacesCGContextAddRectCGPathCloseSubpath, which replacesCGContextClosePathWhen you want to append the path to a graphics context, you call the functionCGContextAddPath. The path stays in the graphics context until Quartz paints it. You can add the path again by callingCGContextAddPath.

You can replace the path in a graphics context with the stroked version of the path by calling the functionCGContextReplacePathWithStrokedPath.

【Painting a Path】

Functions for Stroking a Path:

Filling a Path

【Clipping to a Path】

Thecurrent clipping areais created from a path that serves as a mask, allowing you to block out the part of the page that you don’t want to paint. For example, if you have a very large bitmap image and want to show only a small portion of it, you could set the clipping area to display only the portion you want to show.

When you paint, Quartz renders paint only within the clipping area. Drawing that occurs inside the closed subpaths of the clipping area is visible; drawing that occurs outside the closed subpaths of the clipping area is not.

1CGContextBeginPath (context);2CGContextAddArc (context, w/2, h/2, ((w>h) ? h : w)/2,0,2*PI,0);3CGContextClosePath (context);4CGContextClip (context);

View Code

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航