Using CABasicAnimation to rotate a UIImageView more than once

tl;dr: It is very easy to misuse removeOnCompletion = NO and most people don’t realize the consequences of doing so. The proper solution is to change the model value “for real”.


First of all: I’m not trying to judge or be mean to you. I see the same misunderstanding over and over and I can see why it happens. By explaining why things happen I hope that everyone who experience the same issues and sees this answer learn more about what their code is doing.

What went wrong

I was under the impression that animation.additive = YES; would cause further animations to use the current state as a starting point.

That is very true and it’s exactly what happens. Computers are funny in that sense. They always to exactly what you tell them and not what you want them to do.

removeOnCompletion = NO can be a bitch

In your case the villain is this line of code:

animation.removedOnCompletion = NO;

It is often misused to keep the final value of the animation after the animation completes. The only problem is that it happens by not removing the animation from the view. Animations in Core Animation doesn’t alter the underlying property that they are animating, they just animate it on screen. If you look at the actual value during the animation you will never see it change. Instead the animation works on what is called the presentation layer.

Normally when the animation completes it is removed from the layer and the presentation layer goes away and the model layer appears on screen again. However, when you keep the animation attached to the layer everything looks as it should on screen but you have introduced a difference between what the property says is the transform and how the layer appears to be rotated on screen.

When you configure the animation to be additive that means that the from and to values are added to the existing value, just as you said. The problem is that the value of that property is 0. You never change it, you just animate it. The next time you try and add that animation to the same layer the value still won’t be changed but the animation is doing exactly what it was configured to do: “animate additively from the current value of the model“.

The solution

Skip that line of code. The result is however that the rotation doesn’t stick. The better way to make it stick is to change the model. Set the new end value of the rotation before animating the rotation so that the model looks as it should when the animation gets removed.

byValue is like magic

There is a very handy property (that I’m going to use) on CABasicAnimation that is called byValue that can be used to make relative animations. It can be combined with either toValue and fromValue to do many different kinds of animations. The different combinations are all specified in its documentation (under the section). The combination I’m going to use is:

byValue and toValue are non-nil. Interpolates between (toValue - byValue) and toValue.

Some actual code

With an explicit toValue of 0 the animation happens from “currentValue-byValue” to “current value”. By changing the model first current value is the end value.

NSString *zRotationKeyPath = @"transform.rotation.z"; // The killer of typos

// Change the model to the new "end value" (key path can work like this but properties don't)
CGFloat currentAngle = [[_myview.layer valueForKeyPath:zRotationKeyPath] floatValue];
CGFloat angleToAdd   = M_PI_2; // 90 deg = pi/2
[_myview.layer setValue:@(currentAngle+angleToAdd) forKeyPath:zRotationKeyPath]; 

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:zRotationKeyPath];
animation.duration = 10;
// @( ) is fancy NSNumber literal syntax ...
animation.toValue = @(0.0);        // model value was already changed. End at that value
animation.byValue = @(angleToAdd); // start from - this value (it's toValue - byValue (see above))

// Add the animation. Once it completed it will be removed and you will see the value
// of the model layer which happens to be the same value as the animation stopped at.
[_myview.layer addAnimation:animation forKey:@"90rotation"];

Small disclaimer:

I didn’t run this code but am fairly certain that it runs as it should and that I didn’t do any typos. Correct me if I did. The entire discussion is still valid.

Leave a Comment