Graphics Reference
In-Depth Information
//apply easing
time = bounceEaseOut(time);
//interpolate position
id
position = [
self
interpolateFromValue:
self
.fromValue
toValue:
self
.toValue
time:time];
//move ball view to new position
self
.ballView.center = [position CGPointValue];
//stop the timer if we've reached the end of the animation
if
(
self
.timeOffset >=
self
.duration)
{
[
self
.timer invalidate];
self
.timer =
nil
;
}
}
@end
Run Loop Modes
Notice that when we create the
CADisplayLink
, we are required to specify a
run loop
and
run loop mode.
For the run loop, we've used the main run loop (the run loop hosted by the
main thread) because any user interface updates should always be performed on the main
thread. The choice of mode is less clear, though. Every task that is added to the run loop has
a mode that determines its priority. To ensure that the user interface remains smooth at all
times, iOS will give priority to user interface related tasks and may actually stop executing
other tasks altogether for a brief time if there is too much UI activity.
A typical example of this is when you are scrolling using a
UIScrollView
. During the
scroll, redrawing the scrollview content takes priority over other tasks, so standard
NSTimer
and network events may not fire while this is happening. Some common choices for the run
loop mode are as follows:
▪
NSDefaultRunLoopMode
—The standard priority
▪
NSRunLoopCommonModes
—High priority
▪
UITrackingRunLoopMode
—Used for animating
UIScrollView
and other controls
In our example, we have used
NSDefaultRunLoopMode
, but to ensure that our animation
runs smoothly, we could use
NSRunLoopCommonModes
instead. Just be cautious when using
this mode because when your animation is running at a high frame rate, you may find that