Graphics Reference
In-Depth Information
Global Versus Local Time
Core Animation has a concept of global time, also known as mach time (“mach” being the
name for the system kernel on iOS and Mac OS). Mach time is global only in the sense that
it is the same across all processes on the device—it isn't universal across different
devices—but that is sufficient to make it useful as a reference point for animations. To
access mach time, you can use the CACurrentMediaTime function, as follows:
CFTimeInterval time = CACurrentMediaTime ();
The absolute value returned by this function is largely irrelevant (it reflects the number of
seconds that the device has been awake since it was last rebooted, which is unlikely to be
something you care about), but its purpose is to act as a relative value against which you
can make timing measurements. Note that mach time pauses when the device is asleep,
which means that all CAAnimations (which depend on mach time) will also pause.
For this reason, mach time is not useful for making long-term time measurements. It would
be unwise to use CACurrentMediaTime to update a real-time clock, for example. (You can
poll the current value of [NSDate date] for that purpose instead, as we did in our clock
example in Chapter 3.)
Each CALayer and CAAnimation instance has its own local concept of time, which may
differ from global time depending on the beginTime , timeOffset , and speed properties of
the parent objects in the layer/animation hierarchy. Just as there are methods to convert
between different layers' local spatial coordinate systems, CALayer also has methods to
convert between different layers' local time frames. These are as follows:
- ( CFTimeInterval )convertTime:(CFTimeInterval)t fromLayer:(CALayer *)l;
- ( CFTimeInterval )convertTime:(CFTimeInterval)t toLayer:(CALayer *)l;
These methods can be useful if you are trying to synchronize animations between multiple
layers that do not share the same speed , or have nonzero timeOffset or beginTime values.
Pause, Rewind, and Fast-Forward
Setting an animation's speed property to zero will pause it, but it's not actually possible to
modify an animation after it has been added to a layer, so you can't use this property to
pause an animation that's in progress. Adding a CAAnimation to a layer makes an
immutable copy of the animation object; so changing the properties of the original
animation has no effect on the one actually attached to the layer. Conversely, retrieving the
in-progress animation directly from the layer by using -animationForKey: will give you
the correct animation object, but attempting to modify its properties will throw an
exception.
If you remove an in-progress animation from its layer, the layer will snap back to its pre-
animated state. But if you copy the property values from the presentation layer to the model
layer before removing the animation, the animation will appear to have paused. The
disadvantage of this is that you cannot easily resume the animation again later.
Search WWH ::




Custom Search