Graphics Reference
In-Depth Information
@property
(
nonatomic
,
weak
)
IBOutlet
UIView
*layerView;
@end
@implementation
ViewController
- (
void
)viewDidLoad
{
[
super
viewDidLoad
];
//test layer action when outside of animation block
NSLog
(
@"Outside: %@"
, [
self
.layerView
actionForLayer
:
self
.layerView.
layer
forKey
:
@"backgroundColor"
]);
//begin animation block
[
UIView
beginAnimations
:
nil
context
:
nil
];
//test layer action when inside of animation block
NSLog
(
@"Inside: %@"
, [
self
.layerView
actionForLayer
:
self
.layerView.
layer
forKey
:
@"backgroundColor"
]);
//end animation block
[
UIView
commitAnimations
];
}
@end
When we run the project, we see this in the console:
$ LayerTest[21215:c07] Outside: <null>
$ LayerTest[21215:c07] Inside: <CABasicAnimation: 0x757f090>
So as predicted,
UIView
is disabling implicit animation when properties are changed
outside of an animation block by returning
nil
for the property actions. The action that it
returns when animation is enabled depends on the property type, but in this case, it's a
CABasicAnimation
. (You'll learn what that is in Chapter 8, “Explicit Animations”.)
Returning
nil
for the action is not the only way to disable implicit animations;
CATransaction
has a method called
+setDisableActions:
that can be used to
enable or disable implicit animation for all properties simultaneously. If we modify the
code in Listing 7.2 by adding the following line after
[CATransaction begin]
, it
will prevent any animations from taking place:
[
CATransaction
setDisableActions:
YES
];