使用CABasicAnimation旋转CALayer后,图层会跳回到未旋转的位置 [英] After rotating a CALayer using CABasicAnimation the layer jumps back to it's unrotated position

查看:164
本文介绍了使用CABasicAnimation旋转CALayer后,图层会跳回到未旋转的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力创造一枚落硬币。硬币图像是一个带有2个CABasicAnimations的CALayer - 一个倒下和一个旋转一个。当落下的动画结束时,它就会停留在那里。旋转动画虽然应该是随机的,但每次都以不同的角度结束,只会弹回原来的CALAyer图像。

I am trying to create a falling coin. The coin image is a CALayer with 2 CABasicAnimations on it - a falling down and a rotation one. When the falling down animation gets to its end, it stays there. The rotation animation though, which is supposed to be random and end up in a different angle every time, just pops back to the original CALAyer image.

我希望它留下来在它完成动画的角度。可能吗?我该怎么办?

I want it to stay in the angle it finished the animation on. Is it possible? How do I do it?

代码:

//Moving down animation:
CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
anim.duration = 1;
anim.autoreverses = NO;
anim.removedOnCompletion = YES;

anim.fromValue = [NSNumber numberWithInt: -80 - row_height * (8 - _col)];
anim.toValue = [NSNumber numberWithInt: 0];

//Rotation Animation:
CABasicAnimation *rota = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
rota.duration = 4;
rota.autoreverses = NO;
rota.removedOnCompletion = NO;
rota.fromValue = [NSNumber numberWithFloat: 0];
rota.toValue = [NSNumber numberWithFloat: 2.5 * 3.15 ];
[cl addAnimation: rota forKey: @"rotation"];
[cl addAnimation: anim forKey: @"animateFalling"];


推荐答案

您是否已将旋转动画的removedOnCompletion属性设置为不,例如,

Have you set the removedOnCompletion property of the rotation animation to NO, e.g.,

rota.removedOnCompletion = NO;

这应该使表示层离开动画结束时的位置。默认值为YES,它将回溯到模型值,即您描述的行为。

That should leave the presentation layer where it was when the animation finished. The default is YES, which will snap back to the model value, i.e., the behavior you describe.

还应设置fillMode,即

The fillMode should also be set, i.e.,

rota.fillMode = kCAFillModeForwards;

这篇关于使用CABasicAnimation旋转CALayer后,图层会跳回到未旋转的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆