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;
124 switch (anAnimationCurve)
144 reason:@"Invalid value provided for animation curve"];
148 _animationCurve = anAnimationCurve;
155 - (CPAnimationCurve)animationCurve
157 return _animationCurve;
165 - (void)setDuration:(CPTimeInterval)aDuration
170 _duration = aDuration;
176 - (CPTimeInterval)duration
186 - (void)setFrameRate:(
float)frameRate
191 _frameRate = frameRate;
214 - (void)setDelegate:(
id)aDelegate
216 _delegate = aDelegate;
224 - (void)startAnimation
227 if (_timer || _delegate && [_delegate respondsToSelector:
@selector(animationShouldStart:)] && ![_delegate animationShouldStart:
self])
230 if (_progress === 1.0)
234 _lastTime =
new Date();
236 var timerInterval = _frameRate <= 0.0 ? 0.0001 : 1.0/_frameRate;
246 var currentTime =
new Date(),
247 progress = MIN(1.0, [
self currentProgress] + (currentTime - _lastTime) / (_duration * 1000.0));
249 _lastTime = currentTime;
255 if (progress === 1.0)
260 if ([_delegate respondsToSelector:
@selector(animationDidEnd:)])
261 [_delegate animationDidEnd:self];
268 - (void)stopAnimation
276 if ([_delegate respondsToSelector:
@selector(animationDidStop:)])
277 [_delegate animationDidStop:self];
293 - (void)setCurrentProgress:(
float)aProgress
295 _progress = aProgress;
301 - (float)currentProgress
309 - (float)currentValue
313 if ([_delegate respondsToSelector:
@selector(animation:valueForProgress:)])
314 return [_delegate animation:self valueForProgress:t];
322 [_timingFunction getControlPointAtIndex:1 values:c1];
323 [_timingFunction getControlPointAtIndex:2 values:c2];
342 function sampleCurveX(t)
344 return ((ax * t + bx) * t + cx) * t;
347 function sampleCurveY(t)
349 return ((ay * t + by) * t + cy) * t;
352 function sampleCurveDerivativeX(t)
354 return (3.0 * ax * t + 2.0 * bx) * t + cx;
358 function solveEpsilon(duration)
360 return 1.0 / (200.0 * duration);
363 function solve(x, epsilon)
365 return sampleCurveY(solveCurveX(x, epsilon));
369 function solveCurveX(x, epsilon)
381 x2 = sampleCurveX(t2) - x;
383 if (ABS(x2) < epsilon)
386 d2 = sampleCurveDerivativeX(t2);
407 x2 = sampleCurveX(t2);
409 if (ABS(x2 - x) < epsilon)
418 t2 = (t1 - t0) * 0.5 + t0;
425 bx = 3.0 * (p2x - p1x) - cx;
428 by = 3.0 * (p2y - p1y) - cy;
432 return solve(t, solveEpsilon(duration));