1

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!

Community
  • 1
  • 1
Sudo
  • 991
  • 9
  • 24
  • 1
    Have you looked at the technique suggested by this answer: http://stackoverflow.com/a/19024971/294949. The clever-seeming idea is to add a layer to animate a rect, and use the size of that rect as a measure of progress. – danh Nov 01 '15 at 18:09

1 Answers1

0

Thanks to @danh for pointing me to this answer Core animation progress callback.

It helped me.

I added the subclassed CALayer, animated it the same way I animated my layer (with the same speed variations). That way, I can get the progress of the main animation.

Community
  • 1
  • 1
Sudo
  • 991
  • 9
  • 24