10

I'm animating a UIView's frame property using a CAKeyframeAnimation on the view's CALayer and animating the "position" property. I'd like to be able to halt the animation at it's current state when the user taps the screen, but I'm unable to do so.

I can halt the animation easily enough by calling

[view.layer removeAnimationForKey:kFrameAnimationKey];

The problem is that neither the view's frame not it's layer's position are updated directly by the animation. If I look at the position property at the time the animation starts and when it ends in

- (void)animationDidStop:(CAAnimation *)animation finished:(BOOL)finished

it has not changed.

It seems that you need to do that explicitly when the animation stops. But if the animation stops at some arbitrary point, you don't know how far it's gone. So the question is either how to make the animation update the layer's position property as it goes, or how to know how far the animation has gone when it's been stopped.

1 Answer 1

23

You can achieve this halting of the animation by grabbing the presentationLayer of your animating UIView's layer, then applying its frame to your UIView before removing the animation. For example:

movingView.frame = [[movingView.layer presentationLayer] frame];
[movingView.layer removeAnimationForKey:@"movementAnimation"];

This seems to provide the freezing of the UIView at the current animated position you're looking for.

2
  • Thanks for the answer! it seems the key here is understanding what exactly the presentation layer of a CALayer actually is. I'm not sure they do a great job of explaining that. I'm still not sure what the model layer is either. Feb 16, 2009 at 16:34
  • The "Core Animation Rendering Architecture" section in the "Core Animation Programming Guide" does have a nice diagram of this concept. Basically, the model layer is what you normally set values on, and the presentation layer is a readonly copy that reflects the instantaneous state of the layer.
    – Brad Larson
    Feb 16, 2009 at 18:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.