Graphics Reference
In-Depth Information
representing something like a door that starts to swing closed and then slows to a gradual
stop instead of slamming shut.
The
kCAMediaTimingFunctionEaseInEaseOut
constant creates a gradual acceleration up
to full speed and then a smooth deceleration back down to a stop. This is generally how
most real-world objects move, and is the best choice for most animations. If you could only
ever use one easing function, it would be this one. Given this fact, you might wonder why
this isn't the default, and in fact when you use
UIView
animation methods, this
is
the
default, but when creating a
CAAnimation
, you will need to specify it yourself.
Finally, we have
kCAMediaTimingFunctionDefault
, which is very similar to
kCAMediaTimingFunctionEaseInEaseOut
, but creates a slightly more rapid initial
acceleration up to full speed, followed by a slightly more gradual deceleration. The
difference between this and
kCAMediaTimingFunctionEaseInEaseOut
is almost
imperceptible, but Apple presumably felt that this was a better choice as the default for
implicit animations (although they subsequently changed their mind for UIKit, which uses
kCAMediaTimingFunctionEaseInEaseOut
as the default instead). Remember that despite
the name, this is
not
the default value when creating an
explicit
CAAnimation
, it is only
used as the default for
implicit
animations. (In other words, the default layer action
animations use
kCAMediaTimingFunctionDefault
as their timing function.)
You can try out these different easing functions using a simple test project (see Listing
10.1). Just adjust the timing function in the code before running the project, and then tap
anywhere to see the layer move with the specified easing.
Listing 10.1
Simple Project for Testing Easing Functions
@interface
ViewController ()
@property
(
nonatomic
,
strong
)
CALayer
*colorLayer;
@end
@implementation
ViewController
- (
void
)viewDidLoad
{
[
super
viewDidLoad
];
//create a red layer
self
.colorLayer = [
CALayer
layer
];
self
.colorLayer.
frame
=
CGRectMake
(
0
,
0
,
100
,
100
);
self
.colorLayer.
position
=
CGPointMake
(
self
.
view
.
bounds
.
size
.
width
/
2.0
,
self
.
view
.
bounds
.
size
.
height
/
2.0
);
self
.colorLayer.
backgroundColor
= [
UIColor
redColor
].
CGColor
;
[
self
.
view
.
layer
addSublayer
:
se
lf
.colorLayer];
}