29 - (BOOL)animationShouldStart:(
CPAnimation)animation;
30 - (float)animation:(
CPAnimation)animation valueForProgress:(
float)progress;
42 @
typedef CPAnimationCurve
80 CPTimeInterval _lastTime;
81 CPTimeInterval _duration;
83 CPAnimationCurve _animationCurve;
89 id <CPAnimationDelegate> _delegate;
91 unsigned _implementedDelegateMethods;
100 - (id)initWithDuration:(
float)aDuration animationCurve:(CPAnimationCurve)anAnimationCurve
107 _duration = MAX(0.0, aDuration);
121 - (void)setAnimationCurve:(CPAnimationCurve)anAnimationCurve
123 var timingFunctionName;
125 switch (anAnimationCurve)
145 reason:@"Invalid value provided for animation curve"];
149 _animationCurve = anAnimationCurve;
156 - (CPAnimationCurve)animationCurve
158 return _animationCurve;
166 - (void)setDuration:(CPTimeInterval)aDuration
171 _duration = aDuration;
177 - (CPTimeInterval)duration
187 - (void)setFrameRate:(
float)frameRate
192 _frameRate = frameRate;
215 - (void)setDelegate:(
id <CPAnimationDelegate>)aDelegate
217 if (_delegate === aDelegate)
220 _delegate = aDelegate;
221 _implementedDelegateMethods = 0;
223 if ([_delegate respondsToSelector:
@selector(animationShouldStart:)])
226 if ([_delegate respondsToSelector:
@selector(animationDidEnd:)])
229 if ([_delegate respondsToSelector:
@selector(animationDidStop:)])
232 if ([_delegate respondsToSelector:
@selector(animation:valueForProgress:)])
241 - (void)startAnimation
244 if (_timer || ![
self _sendDelegateAnimationShouldStart])
247 if (_progress === 1.0)
251 _lastTime =
new Date();
253 var timerInterval = _frameRate <= 0.0 ? 0.0001 : 1.0 / _frameRate;
263 var currentTime =
new Date(),
264 progress = MIN(1.0, [
self currentProgress] + (currentTime - _lastTime) / (_duration * 1000.0));
266 _lastTime = currentTime;
272 if (progress === 1.0)
277 [
self _sendDelegateAnimationDidEnd];
284 - (void)stopAnimation
292 [
self _sendDelegateAnimationDidStop];
308 - (void)setCurrentProgress:(
float)aProgress
310 _progress = aProgress;
316 - (float)currentProgress
324 - (float)currentValue
328 if ([
self _delegateRespondsToAnimationValueForProgress])
329 return [
self _sendDelegateAnimationValueForProgress:t];
337 [_timingFunction getControlPointAtIndex:1 values:c1];
338 [_timingFunction getControlPointAtIndex:2 values:c2];
352 - (BOOL)_delegateRespondsToAnimationValueForProgress
361 - (BOOL)_sendDelegateAnimationShouldStart
366 return [_delegate animationShouldStart:self];
373 - (float)_sendDelegateAnimationValueForProgress:(
float)aProgress
378 return [_delegate animation:self valueForProgress:aProgress];
385 - (void)_sendDelegateAnimationDidEnd
390 [_delegate animationDidEnd:self];
397 - (void)_sendDelegateAnimationDidStop
402 [_delegate animationDidStop:self];
419 function sampleCurveX(t)
421 return ((ax * t + bx) * t + cx) * t;
424 function sampleCurveY(t)
426 return ((ay * t + by) * t + cy) * t;
429 function sampleCurveDerivativeX(t)
431 return (3.0 * ax * t + 2.0 * bx) * t + cx;
435 function solveEpsilon(duration)
437 return 1.0 / (200.0 * duration);
440 function solve(x, epsilon)
442 return sampleCurveY(solveCurveX(x, epsilon));
446 function solveCurveX(x, epsilon)
458 x2 = sampleCurveX(t2) - x;
460 if (ABS(x2) < epsilon)
463 d2 = sampleCurveDerivativeX(t2);
484 x2 = sampleCurveX(t2);
486 if (ABS(x2 - x) < epsilon)
495 t2 = (t1 - t0) * 0.5 + t0;
502 bx = 3.0 * (p2x - p1x) - cx;
505 by = 3.0 * (p2y - p1y) - cy;
509 return solve(t, solveEpsilon(duration));
Used to implement exception handling (creating & raising).
void setAnimationCurve:(CPAnimationCurve anAnimationCurve)
var CPAnimationDelegate_animation_valueForProgress_
void raise:reason:(CPString aName, [reason] CPString aReason)
var CPAnimationDelegate_animationDidStop_
var CPAnimationDelegate_animationShouldStart_
CPTimer scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:(CPTimeInterval seconds, [target] id aTarget, [selector] SEL aSelector, [userInfo] id userInfo, [repeats] BOOL shouldRepeat)
var CPAnimationDelegate_animationDidEnd_
CPAnimationCurve CPAnimationEaseInOut
A timer object that can send a message after the given time interval.
void setCurrentProgress:(float aProgress)