如何在CABasicAnimation

时间:2016-04-18 20:28:02

标签: ios objective-c cabasicanimation

我希望我的星球在离太阳更近的时候加速它的运动,在远离太阳的时候放慢速度......请帮助我!谢谢

CABasicAnimation* rotationAnimation;

rotationAnimation=[CABasicAnimationanimationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.toValue = [NSNumber numberWithFloat: -M_PI * 2.0 /* full rotation*/ * 1/period ];//multiply more to add speed
rotationAnimation.duration = 15;
rotationAnimation.cumulative = YES;
rotationAnimation.repeatCount =  HUGE_VALF;
[planet.layer addAnimation:rotationAnimation forKey:@"orbit"];

1 个答案:

答案 0 :(得分:3)

您想使用CAMediaTimingFunction。查看pre-defined timing functions。您可以将其中一个分配给rotationAnimation.timingFunction。听起来你想要kCAMediaTimingFunctionEaseInEaseOut。我假设这个星球的起点远离太阳。在代码中,它看起来像这样:

夫特:

rotationAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)

目标-C:

rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseInEaseOut];