【问题标题】:CABasicAnimation - switch from one animation to another. How to get currentValue of animation?CABasicAnimation - 从一个动画切换到另一个。如何获取动画的currentValue?
【发布时间】:2015-10-14 09:35:50
【问题描述】:

您好,我正在使用 CABasicAnimation 为 CAShapeLayer 中的路径设置动画。当用户点击我调用的按钮时:

CABasicAnimation *expandAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
    expandAnimation.toValue = (id)[UIBezierPath bezierPathWithOvalInRect:CGRectMake(0,0,30,30)].CGPath;
    expandAnimation.duration = 1.0;
    expandAnimation.fillMode = kCAFillModeBoth;
    expandAnimation.removedOnCompletion = NO;
    [self.circleShape addAnimation:expandAnimation forKey:expandAnimation.keyPath];

当释放按钮时,我会像这样对其进行动画处理:

 CABasicAnimation *narrowAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
    narrowAnimation.fromValue = (id)[UIBezierPath bezierPathWithOvalInRect:CGRectMake(0,0,30,30)].CGPath;
    narrowAnimation.toValue = (id)[UIBezierPath bezierPathWithOvalInRect:CGRectMake(5, 5, 20, 20)].CGPath;
    narrowAnimation.duration = .5;
    narrowAnimation.fillMode = kCAFillModeBoth;
    narrowAnimation.removedOnCompletion = NO;
    [self.circleShape addAnimation:narrowAnimation forKey:narrowAnimation.keyPath];

有时当我开始第二个动画时,第一个动画仍在动画。我到底如何才能获得第一个动画的当前状态,以便我可以将第二个动画的起始值设置为从该状态开始?

对于我使用 BezierPaths 的值。

【问题讨论】:

  • 在做下一个动画之前你有没有尝试[layer removeAnimationForKey:@"path"];
  • 这不是我认为的问题。当我调用第二个动画时,第一个动画会自动停止。我只想让圆圈从当前状态开始缩小,而不是从最大尺寸开始。
  • 谢谢,我现在明白这个问题了。

标签: ios objective-c animation cashapelayer cabasicanimation


【解决方案1】:

您可以使用presentationLayer,它返回表示层对象的副本,该对象代表当前在屏幕上显示的层的状态。

因此,通过使用self.circleShape.presentationLayer,您将获得当前显示在屏幕上的图层。

编辑: 所以解决方案是

narrowAnimation.fromValue = [self.circleShape.presentationLayer valueForKeyPath:@"path"];

【讨论】:

  • 我认为这是要走的路。但是如何访问路径属性?
  • hm.. 你可以得到它的位置并从那个位置开始第二个动画
  • CAShapeLayer 有一个path 属性,这不是您需要的吗?
  • 令人惊讶的是,这确实是正确的方法。我会发布答案。
  • 或者只是将此代码添加到您的答案中,我可以选择您的答案作为正确的答案。
猜你喜欢
  • 1970-01-01
  • 2023-03-20
  • 1970-01-01
  • 2016-11-29
  • 1970-01-01
  • 1970-01-01
  • 2017-05-30
  • 1970-01-01
  • 2017-11-25
相关资源
最近更新 更多