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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With