Graphics Reference
In-Depth Information
}
@end
Figure 6.6
A two-color diagonal gradient using
CAGradientLayer
Multipart Gradients
The
colors
array can contain as many colors as you like, so it is simple to create a
multipart gradient such as a rainbow. By default, the colors in the gradient will be evenly
spaced, but we can adjust the spacing using the
locations
property.
The
locations
property is an array of floating-point values (boxed as
NSNumber
objects). These values define the positions for each distinct color in the colors array, and are
specified in unit coordinates, with 0.0 representing the start of the gradient and 1.0
representing the end.
It is not obligatory to supply a
locations
array, but if you do, you must ensure that the
number of locations matches the number of colors or you'll get a blank gradient.
Listing 6.7 shows a modified version of the diagonal gradient code from Listing 6.6. We
now have a three-part gradient from red to yellow to green. A
locations
array has been
specified with the values 0.0, 0.25, and 0.5, which causes the gradient to be squashed up
toward the top-left corner of the view (see Figure 6.7).