Graphics Reference
In-Depth Information
Listing 7.1
Randomizing the Layer Color
@interface
ViewController ()
@property
(
nonatomic
,
weak
)
IBOutlet
UIView
*layerView;
@property
(
nonatomic
,
weak
)
IBOutlet
CALayer
*colorLayer;
@end
@implementation
ViewController
- (
void
)viewDidLoad
{
[
super
viewDidLoad
];
//create sublayer
self
.colorLayer = [
CALayer
layer
];
self
.colorLayer.
frame
=
CGRectMake
(
50.0f
,
50.0f
,
100.0f
,
100.0f
);
self
.colorLayer.
backgroundColor
= [
UIColor
blueColor
].
CGColor
;
//add it to our view
[
self
.layerView.
layer
addSublayer
:
self
.colorLayer];
}
- (
IBAction
)changeColor
{
//randomize the layer background color
CGFloat
red =
arc4random
() / (
CGFloat
)
INT_MAX
;
CGFloat
green =
arc4random
() / (
CGFloat
)
INT_MAX
;
CGFloat
blue =
arc4random
() / (
CGFloat
)
INT_MAX
;
self
.colorLayer.
backgroundColor
= [
UIColor
colorWithRed
:red
green
:green
blue
:blue
alpha
:
1.0
].
CGColor
;
}
@end