Graphics Reference
In-Depth Information
other tasks such as timers or other iOS animations such as scrolling will stop updating until
your animation has finished.
It's possible to use a CADisplayLink with multiple run loop modes at the same time, so we
could add it to both NSDefaultRunLoopMode and UITrackingRunLoopMode to ensure that it
is not disrupted by scrolling, without interfering with the performance of other UIKit
control animations, like this:
self .timer = [ CADisplayLink displayLinkWithTarget: self
selector: @selector (step:)];
[ self .timer addToRunLoop:[ NSRunLoop mainRunLoop]
forMode: NSDefaultRunLoopMode ];
[ self .timer addToRunLoop:[ NSRunLoop mainRunLoop]
forMode: UITrackingRunLoopMode ];
Like CADisplayLink , NSTimer can also be configured to use different run loop modes by
using this alternative setup code instead of the +scheduledTimerWithTimeInterval:
constructor:
self . timer = [ NSTimer timerWithTimeInterval: 1/60.0
target: self
selector: @selector (step:)
userInfo: nil
repeats: YES ];
[[ NSRunLoop mainRunLoop] addTimer: self . timer
forMode: NSRunLoopCommonModes ];
Physical Simulation
Although we've used our timer-based animation to replicate the behavior of the keyframe
animation from Chapter 10, there is actually a fundamental difference in the way that it
works: In the keyframe implementation, we had to calculate all the frames in advance, but
in our new solution, we are calculating them on demand. The significance of this is that it
means we can modify the animation logic on-the-fly in response to user input, or integrate
with other real-time animation systems such as a physics engine.
Chipmunk
Instead of our current easing-based bounce animation, let's use physics to create a truly
realistic gravity simulation. Simulating physics accurately—even in 2D—is incredibly
complex, so we won't attempt to implement this from scratch. Instead, we'll use an open
source physics library, or engine.
The physics engine we're going to use is called Chipmunk. Other 2D physics libraries are
available (such as Box2D), but Chipmunk is written in pure C instead of C++, making it
easier to integrate into an Objective-C project. Chipmunk comes in various flavors,
 
Search WWH ::




Custom Search