【问题标题】:Animation Along a Path with CAKeyframeAnimation使用 CAKeyframeAnimation 沿路径制作动画
【发布时间】:2015-01-02 14:37:49
【问题描述】:

我的视图中有两个 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 = 130/82,为什么还要使用 X/Y = -100/30?有另一种直接通过图像执行此操作的方法(例如planet.layer.bounds 或planet.frame)吗?

【问题讨论】:

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


    【解决方案1】:

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

    修复它的一种快速方法是在添加动画之前将船移动到行星原点

    ship.center = CGPointMake(87, 173);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-16
      • 1970-01-01
      • 2013-04-29
      • 2012-10-29
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      相关资源
      最近更新 更多