使用CABasicAnimation旋转CALayer后,图层会跳回到未旋转的位置

Mik*_*kle 39 iphone calayer cabasicanimation

我想创造一枚落硬币.硬币图像是一个带有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"];
Run Code Online (Sandbox Code Playgroud)

All*_*net 79

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

rota.removedOnCompletion = NO;
Run Code Online (Sandbox Code Playgroud)

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

还应设置fillMode,即

rota.fillMode = kCAFillModeForwards;
Run Code Online (Sandbox Code Playgroud)

  • 10次​​中有9次,这是错误的答案.您需要实际设置图层的属性.设置fillModeForwards和removedOnCompletion会给出更改的外观,但是,如果在动画结束时查询图层,您将看到其属性仍设置为原始值.这很少(如果有的话)你想要的东西:http://stackoverflow.com/questions/3581804/uiimageview-is-disappearing-after-cakeyframeanimation/3586433#3586433 (18认同)
  • fillMode的东西解决了它.天知道为什么需要:) (10认同)
  • 我同意@MattLong.将`removedOnCompletion`设置为`NO`特别糟糕,因为它使动画保持在空闲状态,永远注定无限循环. (2认同)