18 Nov 2008, 22:22

Bryan Wills (10 posts)

Trying to animate a CALayer. It animates fine, but jumps back to the starting point when the animation is complete.

I tried using [slideOpenLeftAnimation setRemovedOnCompletion:NO];

but that didn’t seem to work. Any ideas? I just want it to leave the layer where it is when the animation ends.


18 Nov 2008, 22:44

Bill Dudney (917 posts)

I have a great idea, buy my Core Animation Book :)

seriously though this happens when you don’t change the value but instead just add the animation.

So don’t add an animation to the layer instead add it to the actions dictionary and then just change the value.

i.e. myLayer.actions = [NSDictionary dictionaryWithObject:slideOpenLeftAnimation forKey:@”position”]; myLayer.position = CGPointMake(100.0f, 100.0f);

Good luck!

19 Nov 2008, 01:35

Bryan Wills (10 posts)

AH! that makes sense. Thank you. And don’t worry, I bought your iPhone book, the Core Animation book, the Cocoa book, heck even the Hello Android book! I attended the Cocoa class last month in Denver and am already signed up for your iPhone class in January.

This syntax is like Klingon to me, and coming from Flex/Flash I am a bit spoiled when it comes to animation. I realize that CA is extremely powerful and I will probably come to appreciate it but it just seems like a lot of work to do really simple things.

Thanks again for your help and patience Bill. Looking forward to the remaining chapters of the iphone book. -B

19 Nov 2008, 12:07

Bill Dudney (917 posts)

Of course, I should have recognized the name :)

It is a real pain to get over the hump but once you do I promise it won’t fell like ‘tons of work’!

Good luck and looking forward to having you in class in Jan!

26 Mar 2009, 03:14

Corey Floyd (2 posts)

Bill, I’m running into a similar situation. Of course I followed your advice (and looked through your book examples), but now my animation no longer executes.

So I either get no animation, or the view returns to its pre-animation position!

I’m using a key frame animation, does that matter?

I have tried adding the animation for opacity and position keys, but neither works. I’m sure it is something simple.

Thanks, Corey

26 Mar 2009, 12:48

Bill Dudney (917 posts)

Hi Corey,

Remember that when you set a value on a layer that you are changing that value immediately. But the animation runs on the presentation layer. Once the animation ends the presentation layer goes away and the ‘model’ layer comes back.

So if you want a view to be in the new position, sets its position to the new location. Don’t add the animation but instead put the animation into the search path (like the actions dictionary for example) and then the view will pick up your animation when you change the position.

Please let me know if any of this does not make sense, and good luck!

02 Apr 2009, 07:23

Corey Floyd (2 posts)

Thanks Bill, I hope your enjoying your trip and NSConference.

I worked around this. I ended up animating it off screen, ha.

I think, however, I understand, but let me say it back you.

  1. Calculate my CGPath.
  2. Add the path to the animation.
  3. Add the animation to the actions dictionary (for @”position” key).
  4. Set the position of the layer to the final position (equal to the last calculated point of the CGPath).
  5. The animation will run and will remain at the final position.

Just a clarification (and thanks for the help so far). On the iPhone, if animate and set the final position of a CALayer which backs a UIView, is the center of the UIView set to the new position of the CALayer, or do I have to set the center of the UIView to the position of the CALayer (provided that the anchor point of the layer is its center)?

By the way, I have really enjoyed your book, and even more so your talks on Late Night Cocoa and the MDRT. They have really helped me (begin to) get my head around core animation and the iPhone.

22 Apr 2009, 13:22

Bill Dudney (917 posts)

Yikes Corey,

Sorry to be so late getting back to you!

NSConference was great, if you can make it next time its worth it for sure!

The steps are correct, however I would not try this with a layer that is ‘owned’ by a view. Instead put your own layer in there so that the view knows you own it. Or just build the UI with CALayers instead of UIViews if the event handling is not too much of a hassle.

17 Sep 2010, 10:20

Charlene (1 post)


I am trying to do the same thing but unfortunately its not working.

CABasicAnimation *rotationAnimation; rotationAnimation = [CABasicAnimation animationWithKeyPath:@”transform.rotation.z”]; rotationAnimation.toValue = [NSNumber numberWithFloat:angleRadians]; rotationAnimation.duration = 0.3; rotationAnimation.cumulative = YES; self.layer.actions = [NSDictionary dictionaryWithObject:rotationAnimation forKey:@”rotationAnimation”];

The above is my code and setting the layer.actions makes nothing work whereas: [self.layer addAnimation:rotationAnimation forKey:@”rotationAnimation”]; works but refreshes the image to where it started from.

Any help please?

17 Oct 2010, 05:19

Bill Dudney (917 posts)

Hi Charleen,

Sorry to be so late getting back to you.

You can find info about this on page 35 in the PDF of the 2nd printing.

But basically the animations do not set the value, they only change where it the view appears to be. When the animation ends the value is not changed so the view goes back to the old value.

24 Oct 2011, 12:12

Charlie (1 post)

Hi Bill, How can we make multiple animations with different images , Since I am new to this coreanimation, please suggest any idea on this.

  You must be logged in to comment