获取当前的CAAnimation转换值

我想访问在某个时间点获取变换比例的值。 这是动画创作:

CABasicAnimation *grow = [CABasicAnimation animationWithKeyPath:@"transform.scale"]; grow.toValue = @1.5f; grow.duration = 1; grow.autoreverses = YES; grow.repeatCount = HUGE_VALF; [view.layer addAnimation:grow forKey:@"growAnimation"]; 

我希望,例如当用户按下按钮时,视图的当前大小。 记录帧或边界始终返回常量值。 任何帮助将非常感激 !

CALayer文档非常清楚地描述了presentationLayer

此方法返回的图层对象提供了当前正在屏幕上显示的图层的近似值。 当动画正在进行时,您可以检索此对象并使用它来获取这些动画的当前值。

在Core Animation中,如果动画在图层上“飞行”,则该图层具有第二层属性,称为presentationLayer。 该图层包含飞行中动画的值。

编辑

(向Mohammed致敬,他花时间为Swift提供替代答案)

使用这样的代码:

Objective-C的

 CGFloat currentScale = [[layer.presentationLayer valueForKeyPath: @"transform.scale"] floatValue]; 

迅速:

 let currentScale = layer.presentation()?.value(forKeyPath: "transform.scale") ?? 0.0 

请注意,如果图层没有表示层,则上面的Swift代码将返回0。 这是一个你应该编程的失败案例。 最好留一个可选的并检查零回报。

快速版的@Duncan C答案将是:

 let currentValue = someView.layer.presentation()?.value(forKeyPath: "transform.scale")