当 UIViewPropertyAnimator 正在执行动画过程时,如何增加一个值?

How can I increment a value while UIViewPropertyAnimator is going through the animation process?

如何在 UIViewPropertyAnimator 执行动画过程时增加值?由于我已将我的对象设置为运动状态,因此我想在它落在我的屏幕上时增加它的值,但我不确定如何使用我的 Animator link。有什么建议么?这是我发起

的方式
let toTravel = CGFloat(blockSize * newInstance.distanceToTravel()) //checks how far it needs to go.
let frame = CGRect(x: x, y: startY, width: width, height: height)
super.init(frame: frame)
backgroundColor = UIColor.clear
addSubBlocksToView(grid: grid, blockSize: blockSize)
animator = UIViewPropertyAnimator(duration: 10.0, curve: .linear) { [unowned self] in
    self.center.y += toTravel // need to calculate CODE this it is a static right now
}

如您所见,我计算的距离需要 10 秒。我想跟踪它用计数器移动的每几个像素 (30)。

启动动画时设置一个CADisplayLink。在动画过程中,你可以检查当前位置(假设它是一个线性动画,你可以通过 initialPosition + time/10 * distance 轻松地做到这一点)。动画完成后,您可以使显示无效 link。显示 link 为您提供每一帧的回调,您可以使用它来更新屏幕计数器。

或者您可以考虑使用 CADisplayLink 来驱动整个动画(并使用由您自己的代码驱动的自定义动画曲线。)