80 CPTimeInterval _lastTime;
81 CPTimeInterval _duration;
83 CPAnimationCurve _animationCurve;
99 - (id)initWithDuration:(
float)aDuration animationCurve:(CPAnimationCurve)anAnimationCurve
106 _duration = MAX(0.0, aDuration);
120 - (void)setAnimationCurve:(CPAnimationCurve)anAnimationCurve
122 var timingFunctionName;
123 switch (anAnimationCurve)
138 reason:"Invalid value provided for animation curve"];
142 _animationCurve = anAnimationCurve;
149 - (CPAnimationCurve)animationCurve
151 return _animationCurve;
159 - (void)setDuration:(CPTimeInterval)aDuration
164 _duration = aDuration;
170 - (CPTimeInterval)duration
180 - (void)setFrameRate:(
float)frameRate
185 _frameRate = frameRate;
208 - (void)setDelegate:(
id)aDelegate
210 _delegate = aDelegate;
218 - (void)startAnimation
221 if (_timer || _delegate && [_delegate respondsToSelector:
@selector(animationShouldStart:)] && ![_delegate animationShouldStart:
self])
224 if (_progress === 1.0)
228 _lastTime =
new Date();
238 var currentTime =
new Date(),
239 progress = MIN(1.0, [
self currentProgress] + (currentTime - _lastTime) / (_duration * 1000.0));
241 _lastTime = currentTime;
247 if (progress === 1.0)
252 if ([_delegate respondsToSelector:
@selector(animationDidEnd:)])
253 [_delegate animationDidEnd:self];
260 - (void)stopAnimation
268 if ([_delegate respondsToSelector:
@selector(animationDidStop:)])
269 [_delegate animationDidStop:self];
285 - (void)setCurrentProgress:(
float)aProgress
287 _progress = aProgress;
293 - (float)currentProgress
301 - (float)currentValue
305 if ([_delegate respondsToSelector:
@selector(animation:valueForProgress:)])
306 return [_delegate animation:self valueForProgress:t];
314 [_timingFunction getControlPointAtIndex:1 values:c1];
315 [_timingFunction getControlPointAtIndex:2 values:c2];
334 function sampleCurveX(t)
336 return ((ax * t + bx) * t + cx) * t;
339 function sampleCurveY(t)
341 return ((ay * t + by) * t + cy) * t;
344 function sampleCurveDerivativeX(t)
346 return (3.0 * ax * t + 2.0 * bx) * t + cx;
350 function solveEpsilon(duration)
352 return 1.0 / (200.0 * duration);
355 function solve(x, epsilon)
357 return sampleCurveY(solveCurveX(x, epsilon));
361 function solveCurveX(x, epsilon)
373 x2 = sampleCurveX(t2) - x;
375 if (ABS(x2) < epsilon)
378 d2 = sampleCurveDerivativeX(t2);
399 x2 = sampleCurveX(t2);
401 if (ABS(x2 - x) < epsilon)
410 t2 = (t1 - t0) * 0.5 + t0;
417 bx = 3.0 * (p2x - p1x) - cx;
420 by = 3.0 * (p2y - p1y) - cy;
424 return solve(t, solveEpsilon(duration));