·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> app软件开发 >> IOS开发 >> PathCreationandPathPainting

PathCreationandPathPainting

作者:佚名      IOS开发编辑:admin      更新时间:2022-07-23

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, a clipping area.

  1、Point:CGContextMoveToPoint

  2、Lines:CGContextAddLineToPoint、CGContextAddLines

  3、Arcs:CGContextAddArc、CGContextAddArcToPoint

  4、Curves:CGContextAddCurveToPoint、CGContextAddQuadCurveToPoint

  To close the current subpath, your application should call CGContextClosePath. 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 call CGContextClosePath to 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 called CGContextClosePath to 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 have only a single path in 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 function CGContextBeginPath . Next, you set the starting point for the first shape, or subpath, in the path by calling the function CGContextMoveToPoint. 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 function CGContextBeginPath.
  • Lines, arcs, and curves are drawn starting at the current point. An empty path has no current point; you must call CGContextMoveToPoint to 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 function CGContextClosePath to 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 path

  After 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, Quartz PRovides two data types for creating reusable paths—CGPathRef and CGMutablePathRef. You can call the function CGPathCreateMutable to 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 functions Operate on a CGPath object instead of a graphics context. These functions are:

  • CGPathCreateMutable, which replacesCGContextBeginPath
  • CGPathMoveToPoint, which replaces CGContextMoveToPoint
  • CGPathAddLineToPoint, which replaces CGContextAddLineToPoint
  • CGPathAddCurveToPoint, which replaces CGContextAddCurveToPoint
  • CGPathAddEllipseInRect, which replaces CGContextAddEllipseInRect
  • CGPathAddArc, which replaces CGContextAddArc
  • CGPathAddRect, which replaces CGContextAddRect
  • CGPathCloseSubpath, which replaces CGContextClosePath

  When you want to append the path to a graphics context, you call the function CGContextAddPath. The path stays in the graphics context until Quartz paints it. You can add the path again by calling CGContextAddPath.

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

Painting a Path

  Functions for Stroking a Path:

  

  Filling a Path 

  

Clipping to a Path

  The current clipping area is 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.

  

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