Graphics Reference
In-Depth Information
}
@end
Relative Time
As far as Core Animation is concerned, time is relative. Each animation has its own
representation of time, which can be independently sped up, delayed, or offset.
The
beginTime
property specifies the time delay before the animation begins. This delay is
measured from the point at which the animation is added to a visible layer. This defaults to
zero (that is, the animation will begin immediately).
The
speed
property is a time
multiplier.
It defaults to 1.0, but decreasing it will slow down
time for the layer/animation and increasing it will speed up time. With a speed of 2.0, an
animation with a nominal
duration
of 1 second will actually complete in 0.5 seconds.
The
timeOffset
property is similar to
beginTime
in that it time-shifts the animation. But
while increasing the
beginTime
increases the delay before an animation begins, increasing
the
timeOffset
fast-forwards to a specific point in the animation. For example, for an
animation that lasts 1 second, setting a
timeOffset
of 0.5 seconds would mean that the
animation starts halfway through.
Unlike
beginTime
,
timeOffset
is unaffected by
speed
. So, if you were to increase the
speed
to 2.0 as well as setting the
timeOffset
to 0.5, you would have effectively skipped
to the end of the animation because a 1-second animation sped up by a factor of two lasts
for only 0.5 seconds. However, even if you skip to the end of the animation using the
timeOffset
, it will still play for the same total duration; the animation simply loops around
and plays again up until the point where it originally started.
You can try this out with the simple test app in Listing 9.3. Just set the
speed
and
timeOffset
sliders to the desired value, and then press Play to see the effect they have (see
Figure 9.3).
Listing 9.3
Testing the
timeOffset
and
speed
Properties
@interface
ViewController ()
@property
(
nonatomic
,
weak
)
IBOutlet
UIView
*containerView;
@property
(
nonatomic
,
weak
)
IBOutlet
UILabel
*speedLabel;
@property
(
nonatomic
,
weak
)
IBOutlet
UILabel
*timeOffsetLabel;
@property
(
nonatomic
,
weak
)
IBOutlet
UISlider
*speedSlider;
@property
(
nonatomic
,
weak
)
IBOutlet
UISlider
*timeOffsetSlider;
@property
(
nonatomic
,
strong
)
UIBezierPath
*bezierPath;
@property
(
nonatomic
,
strong
)
CALayer
*shipLayer;