Graphics Reference
In-Depth Information
So to recap, we've learned the following:
▪
UIView
backing layers do not have implicit animation enabled. The only ways to
animate the properties of a backing layer are to use the
UIView
animation methods
(instead of relying on
CATransaction
), to subclass
UIView
itself and override
the
-actionForLayer:forKey:
method, or to create an explicit animation (see
Chapter 8 for details).
▪
For hosted (that is, nonbacking) layers, we can control the animation that will be
selected for an implicit property animation by either implementing the
-actionForLayer:forKey:
layer delegate method, or by providing an
actions
dictionary.
Let's specify a different action for our color fade example. We'll modify Listing 7.1 by
setting a custom
actions
dictionary for
colorLayer
. We could implement this using
the delegate instead, but the
actions
dictionary approach requires marginally less code.
So how can we create a suitable action object?
Actions are usually specified using an
explicit
animation object that will be called
implicitly
by Core Animation when it is needed. The animation we are using here is a
push transition
,
which is implemented with an instance of
CATransition
(see Listing 7.6).
Transitions are explained fully in Chapter 8, but suffice to say for now that
CATransition
conforms to the
CAAction
protocol, and can therefore be used as a
layer action. The result is pretty cool; whenever we change our layer color, the new value
slides in from the left instead of using the default crossfade effect (see Figure 7.3).
Listing 7.6
Implementing a Custom Action
@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
;