Graphics Reference
In-Depth Information
@property ( nonatomic , assign ) CFTimeInterval lastStep;
@property ( nonatomic , strong ) id fromValue;
@property ( nonatomic , strong ) id toValue;
@end
@implementation ViewController
...
- ( void )animate
{
//reset ball to top of screen
self . ballView . center = CGPointMake ( 150 , 32 );
//configure the animation
self .duration = 1.0 ;
self .timeOffset = 0.0 ;
self .fromValue = [NSValue valueWithCGPoint:CGPointMake( 150 , 32 )];
self .toValue = [NSValue valueWithCGPoint:CGPointMake( 150 , 268 )];
//stop the timer if it's already running
[ self .timer invalidate];
//start the timer
self .lastStep = CACurrentMediaTime();
self .timer = [CADisplayLink displayLinkWithTarget: self
selector: @selector (step:)];
[ self .timer addToRunLoop:[NSRunLoop mainRunLoop]
forMode:NSDefaultRunLoopMode];
}
- ( void )step:(CADisplayLink *)timer
{
//calculate time delta
CFTimeInterval thisStep = CACurrentMediaTime();
CFTimeInterval stepDuration = thisStep - self .lastStep;
self .lastStep = thisStep;
//update time offset
self .timeOffset = MIN( self .timeOffset + stepDuration, self .duration);
//get normalized time offset (in range 0 - 1)
float time = self .timeOffset / self .duration;
Search WWH ::




Custom Search