views:

992

answers:

1

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.

+3  A: 

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.

Brad Larson
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.
Rich Bruchal
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