Search code examples
iosquartz-graphicsbezier

draw ios quartz 2d path with a varying alpha component


I'd like to paint some Bezier curves with the alpha channel that is changing during the curve painting. Right now I'm able to draw bezier paths, with a fixed alpha channel. What I'd like to do is to draw a single bezier curve that uses a certain value of the alpha channel for the first n points of the path another, alpha value for the subsequent m points and so on. The code I'm using for drawing bezier path is:

CGContextSetStrokeColorWithColor(context, curva.color.CGColor);
....
CGContextAddCurveToPoint(context, cp1.x, cp1.y, cp2.x, cp2.y, endPoint.x, endPoint.y);
....
CGContextStrokePath(context);

Is there a way to achieve what I'm describing?

Many thanks, Giovanni


Solution

  • There are two ways I imagine you could achieve this effect:

    • If you absolutely must compute the alpha at draw time, you could experiment with creating a colored CGPatternRef ( http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Reference/CGPattern/Reference/reference.html ) whose drawing callback inspects the current context translation and draws a color based on the offset. Instead of setting a stroke color, you'd set CGContextSetStrokePattern to your created pattern.

    • If appropriate for the drawn object, you could precompute the alpha across the space of the curve and use your CGPathRef as a mask for the alpha image.

    I should qualify that I've never attempted the CGPatternRef method, so I can only point that out as a potential, not a certainty.