Graphics Reference
In-Depth Information
NOTE
Random Shaking
You might have noticed in Listing 4-7 that we set the begin time for the animation using a
small random offset. If we don't do this, all the icons shake in sync with each other. To
achieve the same effect as what you see on the iPhone, having this random offset is neces-
sary. We use the current time in seconds to seed the random number generator. Then we
grab that next random number and multiply it by .0000000001 to get a small offset. We set
the animation start time to this value and return the animation. When it gets added to the
layer, it plays back starting at that offset from the time it was actually added to the layer.
Rotation Axis and Layer Geometry
In this example, the rotation of our layer revolves around the center of the layer. This is
the default anchor point for layers ( 0.5, 0.5 ). If you want to rotate around a different
axis, for example the bottom left corner, you need to specify in the layer itself a new
anchor point with a call to -setAnchorPoint:CGPointMake(0.0, 0.0) . For this example,
though, the default is appropriate. Layer geometry is discussed in greater detail in
Chapter 6, “Layer Filters.”
Perfecting the Effect
With some minor changes to the dura-
tion parameter and each of the keyframe
values, we can get it to look just right. If
you run the code in Listing 4-7, you
notice that the shaking effect seems to
run a little too slow. To make this run
faster, simply change the duration field
to something more appropriate. We are
currently telling the animation to complete within half of a second, 0.5 . If you change it
to instead be finished within one-fifteenth of a second (changing setDuration:0.5 to
setDuration:0.15 ), the animation gives you more of the desired effect.
NOTE
If you have an iPhone or iPod touch, from the
Home menu, tap and hold any icon and you
can see the animation run. Now run the Icon
Dance example in Xcode to see how similar
the effect looks.
Adding the Close Box
This part of the project is not related in
particular to keyframe animation, but it
will show you how to complete this
effect and introduce you to drawing in
layer contents.
NOTE
Close Box User Interaction
If you want to receive an event when the
close box is clicked, you need to obtain the
click position from the backing view and then
do a hit test on the layer to see if it is in
range because Core Animation layers don't
themselves receive events. See Chapter 11,
“User Interaction,” for more details.
While the animation is running, you
notice a circular close box with an X
inside. This is intended to indicate that
an application can be deleted on the
Search WWH ::




Custom Search