Kelly Bennett
Kelly Bennett

Reputation: 725

CAKeyFrameAnimation delays before repeating

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);

Upvotes: 7

Views: 1681

Answers (3)

haho
haho

Reputation: 146

Try this:

[animation setCalculationMode:kCAAnimationPaced]

Upvotes: 13

matt
matt

Reputation: 535138

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.

Upvotes: 1

Nirav Bhatt
Nirav Bhatt

Reputation: 6969

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]]

Upvotes: 0

Related Questions