我希望我的星球在离太阳更近时加速运动,而在离太阳更远时减速。.请帮助我!谢谢

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

最佳答案

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

迅速:

rotationAnimation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)

objective-c :
rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseInEaseOut];

10-08 19:32