40

我正在尝试创建一个落下的硬币。硬币图像是一个带有 2 个 CABasicAnimations 的 CALayer - 一个跌落和一个旋转。当跌落动画结束时,它会停留在那里。不过,旋转动画应该是随机的,并且每次都以不同的角度结束,但它只是弹回原始的 CALAyer 图像。

我希望它保持在完成动画的角度。是否可以?我该怎么做?

代码:

//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"];
4

3 回答 3

79

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

rota.removedOnCompletion = NO;

那应该将表示层留在动画完成时的位置。默认值为 YES,它将恢复为模型值,即您描述的行为。

还应该设置 fillMode,即

rota.fillMode = kCAFillModeForwards;
于 2009-09-04T07:21:53.840 回答
4

我试图来回旋转箭头,就像 Twitter/Facebook 的“拉动刷新”效果。

问题是,我在同一个 UIView 上来回旋转,所以在添加之后

rotation.removedOnCompletion = NO;
rotation.fillMode = kCAFillModeForwards;

前向动画战争运行良好,但后向动画根本不起作用。

所以我添加了yeadixon建议的最后一行,另外将视图的变换设置为动画的完成状态:(旋转180度)

[myview.layer removeAllAnimations];
myView.transform = CGAffineTransformMakeRotation(M_PI);

对于“恢复”动画(向后),我在完成时执行此操作:

myView.transform = CGAffineTransformMakeRotation(0);

......它工作正常。不知何故,它不需要 removeAllAnimations 调用。

于 2011-06-28T11:22:55.173 回答
1

我发现通过设置:removedOnCompletion = NO;

没有在仪器中产生可见的泄漏,但没有被释放并且正在积累少量内存。IDK 如果它是我的实现或什么,但通过在动画末尾添加 removeAllAnimations 似乎清除了这一点剩余内存。

[myview.layer removeAllAnimations];
于 2010-11-04T20:38:58.047 回答