Graphics Reference
In-Depth Information
from Listing 7.2 that removes the
colorLayer
and sets the
layerView
backing layer's
background color directly.
Listing 7.4
Setting the Property of the Backing Layer Directly
@interface
ViewController ()
@property
(
nonatomic
,
weak
)
IBOutlet
UIView
*layerView;
@end
@implementation
ViewController
- (
void
)viewDidLoad
{
[
super
viewDidLoad
];
//set the color of our layerView backing layer directly
self
.layerView.
layer
.
backgroundColor
= [
UIColor
blueColor
].
CGColor
;
}
- (
IBAction
)changeColor
{
//begin a new transaction
[
CATransaction
begin
];
//set the animation duration to 1 second
[
CATransaction
setAnimationDuration
:
1.0
];
//randomize the layer background color
CGFloat
red =
arc4random
() / (
CGFloat
)
INT_MAX
;
CGFloat
green =
arc4random
() / (
CGFloat
)
INT_MAX
;
CGFloat
blue =
arc4random
() / (
CGFloat
)
INT_MAX
;
self
.layerView.
layer
.
backgroundColor
= [
UIColor
colorWithRed
:red
green
:green
blue
:blue
alpha
:
1.0
].
CGColor
;
//commit the transaction
[
CATransaction
commit
];
}