I need to calculate the progress of a CABasicAnimation at a time T in order to update a progress view.
When the progress is linear, I simply calculate the elapsed time at each time (thanks to https://stackoverflow.com/a/20993376/2268168).
The thing is, the animation is not linear, the speed is variable.
Speed up
mylayer.timeOffset = [mylayer convertTime:CACurrentMediaTime() fromLayer:nil];
mylayer.beginTime = CACurrentMediaTime();
mylayer.speed=2;
speed = 2;
Slow down
mylayer.timeOffset = [mylayer convertTime:CACurrentMediaTime() fromLayer:nil];
mylayer.beginTime = CACurrentMediaTime();
mylayer.speed=0.5;
speed = 0.5;
How can I calculate the progress of the animation taking into account the variable speed ?
I tried this, but it seems to work only when the speed increases once.
CFTimeInterval elapsedTime = (CACurrentMediaTime() - animation.beginTime);
CFTimeInterval remainingTime = (totalDuration - elapsedTime)/speed;
speed = 1;
totalDuration = remainingTime+elapsedTime;
float progress = (totalDuration-remainingTime)/totalDuration;
Am I doing something wrong ?
Thanks for your help!