Reputation: 45
Hello i want to do a CABasicAnimation rotation where my view rotates 440 degree. After the animatin i don´t want to reset the view to the old position. It should be on the same position like on the last frame of the animation.
CABasicAnimation *rotate = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
rotate.toValue = [NSNumber numberWithFloat:degrees / 180.0 * M_PI];
rotate.duration = 4.4;
[rotate setFillMode:kCAFillModeForwards];
UIView *animationView = [self getBGContainerViewForSender:sender];
[animationView.layer addAnimation:rotate forKey:@"myRotationAnimation"];</i>
Can anybody tell me how to position the view on the last frame?
Upvotes: 0
Views: 156
Reputation:
When you create an animation and add it to a layer, you are not changing any of the values in your object. So in your case, the transform property of animationView will not change. Therefore, when the animation ends, the view will snap back to the old position. To solve this, you have to do two things:
Before you add the animation, set the transform property of the views layer to the value you want when the animation is done. However, this will add an implicit animation to the layer, so you need to kill the implicit animation. To do that:
The value of forKey:
in the add animation method must be the name of the animation, in this case "transform". This name will replace the implicit animation for transform with your explicit animation.
So add:
animationView.layer.transform = CA3DTransformMakeRotate(....);
and change the add animation call to
[animationView.layer addAnimation:rotate forKey:@"transform"];
This is explained in WWDC 2010, Core Animation in Practice Part 1, at about 39:20.
Upvotes: 1
Reputation: 1095
Before initating the animation make a transaction like this:
[CATransaction begin];
[CATransaction setValue:kCFBooleanTrue forKey:kCATransactionDisableActions];
CATransform3D current = animationView.layer.transform;
animationView.layer.transform = CATransform3DRotate(current, numberWithFloat:degrees / 180.0 * M_PI, 0, 1.0, 0);
[CATransaction commit];
/* YOUR ORIGINAL CODE COMES HERE*/
Upvotes: 0