I am doing an indefinite rotation animation, which works perfectly well when I first start it. What I wanted to achieve was to be able to change the rate of rotation at the runtime. I have this function in animationView:
-(void)startBlobAnimation:(float)deltaT
{
[UIView beginAnimations:@"Spinning" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationDuration:deltaT];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationRepeatCount:FLT_MAX];
CGAffineTransform rotation = CGAffineTransformMakeRotation(-symmetryAngle);
blobView.transform = rotation;
// Commit the changes and perform the animation.
[UIView commitAnimations];
}
Calling it with different values of deltaT after the animation was first started doesn't have any effect. If I add [wheelView.layer removeAllAnimations];
at the start of the function then it successfully stops the animation but doesn't restart it. I also tried using the block command to start animation, with the same result. I am totally baffled at this point. Could somebody explain what the problem is? Thanks!
After a long struggle I came up with a solution that seems to work perfectly and give a smooth transition between animations. Basically I just figure out the current angle of rotation and use it to restart the animation at a different rate. One critical point here is in the last line: you MUST have that
anim.keyPath
there - it can't be Nil (as learned from experience). That way the new animation replaces the old animation, I guess. Oh, and to make it more clear: symmetryAngle is a rotation that makes the object look the same, like 72 degrees for 5-fold symmetry.2017
Change the speed of a CA animation as it is running.
Here's exactly how I do the "Alex method" ...
I have a shape layer ...
Here's what happens in
startFastThenSlowDown
...Now, here is a restartable animation thanks to Alex...
In my case, the
thingThatSpins
was a mask (for goodness sake) which was on top of something else (a color gradient, which, in itself animated!)Good one, OP.
A couple issues--
I think for endless animations you will have to use the CoreAnimation API directly.(This is incorrect, you can use UIViewAnimationOptionRepeat)Animations are copied to the layer they are applied to, so you can't change them after that (you can replace them however, but you might see a discontinuity)
layer.speed
to change the effective animation rateHere's a working example. Create a new "empty" iOS project in Xcode and replace your AppDelegate like this: