20.10. Drawing Gradients
Problem
You want to draw gradients on graphics contexts, using different colors.
Solution
Use the CGGradientCreateWithColor function.
Discussion
After learning about colors in Recipe 20.3, we’re ready to put these skills to better use than drawing simple rectangles and colorful text!
Core Graphics allows programmers to create two types of gradients: axial and radial. (We will discuss only axial gradients in this book.) Axial gradients are gradients that start from one point with one color and end at another point with another color (although they can start and stop with the same color, which does not make them much of a gradient). “Axial” means relating to an axis. The two points (start and end point) create a line segment, which will be the axis on which the gradient will be drawn. An example of an axial gradient is shown in Figure 20-27.

Figure 20-27. An axial gradient starting from the color blue and ending in the color green
In order to create an axial gradient, you must call the
CGGradientCreateWithColorComponents function. The return value of this
function will be the new gradient of type CGGradientRef. This is the handle to the
gradient. Once you are done with the gradient, you
must call the CGGradientRelease procedure, passing the
handle to the gradient that you had previously retrieved from CGGradientCreateWithColorComponents.
The CGGradientCreateWithColorComponents ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access