沿CAKeyframeAnimation路径的动画

时间:2015-01-02 14:37:49

标签: objective-c core-animation quartz-core cgpath cakeyframeanimation

我在View中有两个imageViews并与IBOutlet连接:

IBOutlet UIImageView *ship; // X/Y = 130/82, W/H = 61/50
IBOutlet UIImageView *planet;// X/Y = 87/173, W/H = 147/128

我想要做的就是沿椭圆路径(用行星图像创建)为我的船舶图像制作动画,为此:

CAKeyframeAnimation *orbit = [CAKeyframeAnimation animation];
orbit.keyPath = @"position";
orbit.path = CFAutorelease(CGPathCreateWithEllipseInRect(planet.bounds, NULL));
orbit.duration = 4;
orbit.additive = YES;
orbit.repeatCount = HUGE_VALF;
orbit.calculationMode = kCAAnimationPaced;
orbit.rotationMode = kCAAnimationRotateAuto;

[ship.layer addAnimation:orbit forKey:@"orbit"];

问题是代码是我的船舶图像在我的星球图像之外动画,为了解决这个问题,我使用这个手动执行此操作:

CGRect boundingRect = CGRectMake(-100, 30, 200, 200);
orbit.path = CFAutorelease(CGPathCreateWithEllipseInRect(boundingRect, NULL));

现在为什么使用 X / Y = -100/30 如果我的星球在 X / Y = 130/82 ?有另一种方法可以直接通过图像(比如planet.layer.bounds或planet.frame)吗?

1 个答案:

答案 0 :(得分:0)

由于您使用行星边界创建路径,路径将相对于行星位置,但由于您的船不在同一点,它将从其位置开始移动。

解决这个问题的一种快速方法是在添加动画之前将船移动到行星原点

ship.center = CGPointMake(87, 173);
相关问题