7

I have an ball image that I'm animating around a path. The animation is set to repeat forever, but why is there a delay between repeats?

Here's my code:

CGPathRef aPath;
aPath = CGPathCreateWithEllipseInRect(CGRectMake(0, 0, SIZE, SIZE), NULL);

[CATransaction begin];

arcAnimation = [CAKeyframeAnimation animationWithKeyPath: @"position"];
[arcAnimation setBeginTime:CACurrentMediaTime()];
[arcAnimation setDuration: 1.5];
[arcAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
[arcAnimation setAutoreverses: NO];
[arcAnimation setRepeatCount:HUGE_VALF];
arcAnimation.removedOnCompletion = NO;
arcAnimation.fillMode = kCAFillModeRemoved;
[arcAnimation setPath: aPath];
[ball.layer addAnimation: arcAnimation forKey: @"position"];
[CATransaction commit];
CFRelease(aPath);
Kelly Bennett
  • 725
  • 5
  • 27

3 Answers3

13

Try this:

[animation setCalculationMode:kCAAnimationPaced]
Peter DeWeese
  • 18,141
  • 8
  • 79
  • 101
haho
  • 146
  • 2
1

I don't know the actual answer, but you're doing a lot of unnecessary stuff here, and my suggestion would be that you start by removing it. There is no need for the CATransaction block (begin and commit). There is no need to for setBeginTime:. There is definitely no need to set removedOnCompletion and fillMode, as this is not a grouped animation. Just add the animation to the layer and stand back. It will start immediately and will repeat forever, and your code will be simpler and better.

matt
  • 515,959
  • 87
  • 875
  • 1,141
0

You must replace kCAFillModeRemoved with kCAFillModeForwards or some other value. Read documentation on why.

Additionally:

Replace:

[arcAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]]

With (depending on your testing outcome):

[arcAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]]

or

[arcAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]
Nirav Bhatt
  • 6,940
  • 5
  • 45
  • 89
  • Nirav, I definitely do not want to ease in or out. I want the animation to run at a constant speed through the circle without speeding up or slowing down. I've tried all the different fill modes, none have any noticeable effect on the animation. The animation still pauses for a moment at the end before starting again. – Kelly Bennett Feb 01 '13 at 20:32
  • Then I think it is the setBeginTime call that is creating this thing. Since you make a call to it, it takes some time to evaluate the second it should start, which accounts for the delay. Try removing it. – Nirav Bhatt Feb 01 '13 at 20:52
  • I've removed it. Still behaves exactly the same. – Kelly Bennett Feb 01 '13 at 21:10