Wanted to know how can I add transparent dots or lines over CGPath
or NSBezierPath
.
Here are more details about the problem.
I've a solid line say width = 30(drawn using NSBezierPath
or CGPath
) , now I wanted to draw transparent dots over it or transparent lines(thickness=2 or something smaller than 30).

- 1,108
- 10
- 19
2 Answers
You can enumerate the elements of an NSBezierPath or CGPath, and do something for each one.
For NSBezierPath, use elementCount
, elementAtIndex:associatedPoints:
, and a for
loop. The elementAtIndex:associatedPoints:
requires a C array of up to three NSPoint
s.
For CGPath, use CGPathApply
. This takes a pointer to a C function that you have written. One of the two arguments to the function is a structure that contains the same information returned by elementAtIndex:associatedPoints:
, except that it will create the array of points for you.
The element types are mostly the same between them:
A
moveto
orlineto
carries one point.You might wonder why a
lineto
doesn't have two points. The point associated with the element is the destination point—the to inlineto
—that is the new current point immediately afterward. The other point, the one you're coming from, is implicit; in cases where you want to use it, you will simply have to remember the last current point.A (cubic)
curveto
uses all three points.As with
lineto
, the source point is implicit, being simply the last current point. The last point in the array is the destination anchor point; the two other points are the control points.Core Graphics has quadratic
curveto
elements, which only have two points.A cubic
curveto
has two control points and one anchor point; a quadratic one has only one control point and one anchor point.NSBezierPath does not have quadratic
curveto
elements. Allcurveto
elements in an NSBezierPath are cubic.- A
closepath
has no points. It returns to the point of the lastmoveto
.
Either way, for each element, draw whatever anchor-point indicator you want. You could, for example, draw a blue circle at the destination point, and not draw anything for a closepath
(since you already drew that when you encountered the matching moveto
). For curveto
elements, you might also want to draw an indicator for each of the two control points.

- 95,783
- 15
- 211
- 370
-
Thanks for details about CGPath and NSBezierPath. I've already drawn a solid path using point array. Now I wanted to draw transparent dashes or dots or small lines over this solid line. My aim to create a solid line which will look like a paint brush. I've seen the CGContextSetBlendingMode(). But it's not working for me. Thanks. – Omkar Feb 10 '13 at 09:04
-
@Omkar: My answer is for what you're doing, not for simply painting the path. You will need to enumerate the path's elements and draw whatever dash, dot, small line, or other indicator you want for each one. – Peter Hosey Feb 10 '13 at 20:01
-
My requirements are not only to draw dashed lines or dots. I wanted to draw transparent lines or dots over a solid line and the output of drawing transparent dots should make the regin transparent(generally drawing transparent lines over solid(opaque) line gives you the solid line itself, but i wanted to make the regin transparent). I think this problem must be solved with one of the blending mode, but I dont know how blending modes work with transparent surface. I read blending mode, but in all examples they have considered the opaque surfaces/images. – Omkar Feb 11 '13 at 17:15
-
@Omkar: You can use the destination-out blend mode to make transparent what was once solid. – Peter Hosey Feb 11 '13 at 20:56
Use -bezierPathByFlatteningPath
.
Once you have flattened copy of the receiver, compute its length.
Then, iterate through the flattened copy, which is basically an array of points. Keep track of the distance between successive points, so that you can see where you are exactly on the curve.
For example, if you want to draw multiple copies of an object, you have to find on which segment of the flattened copy the object will reside. Once you have found the segment, linear interpolate between the two ends of that segment to find the exact spot.
If this is what you want to achieve, I can elaborate a little and post the category I wrote that does this.

- 3,993
- 4
- 33
- 52