【问题标题】:How can I increment a value while UIViewPropertyAnimator is going through the animation process?如何在 UIViewPropertyAnimator 进行动画处理时增加一个值?
【发布时间】:2017-10-09 02:28:58
【问题描述】:

如何在 UIViewPropertyAnimator 进行动画处理时增加一个值?当我让我的对象运动时,我想在它落在我的屏幕上时增加它的值,但我不确定如何将它与我的 Animator 链接。有什么建议么?这是我如何启动

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)。

【问题讨论】:

    标签: swift uiviewpropertyanimator


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2015-12-24
      • 2020-11-20
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多