Graphics Reference
In-Depth Information
Listing 6.7
Using the
locations
Array to Offset a Gradient
- (
void
)viewDidLoad
{
[
super
viewDidLoad
];
//create gradient layer and add it to our container view
CAGradientLayer
*gradientLayer = [
CAGradientLayer
layer
];
gradientLayer.
frame
=
self
.
containerView
.
bounds
;
[
self
.
containerView
.
layer
addSublayer
:gradientLayer];
//set gradient colors
gradientLayer.
colors
=
@[
(
__bridge
id
)[
UIColor
redColor
].
CGColor
,
(
__bridge
id
)[
UIColor
yellowColor
].
CGColor
,
(
__bridge
id
)[
UIColor
greenColor
].
CGColor
]
;
//set locations
gradientLayer.
locations
=
@[@0.0
,
@0.25
,
@0.5]
;
//set gradient start and end points
gradientLayer.
startPoint
=
CGPointMake
(
0
,
0
);
gradientLayer.
endPoint
=
CGPointMake
(
1
,
1
);
}
Figure 6.7
A three-color gradient, offset to the top left using the
locations
array