结合许多 UIViewPropertyAnimators

Tia*_*ida 7 animation ios uiviewpropertyanimator

我弄乱了UIViewPropertyAnimator的文档,我希望找到一种方法来组合两个 UIViewPropertyAnimator。

像这样的东西:

let firstAnimator = UIViewPropertyAnimator(
   duration: 2, 
   dampingRatio: 0.4, 
   animations: animation1
)

let secondAnimator = UIViewPropertyAnimator(
    duration: 2, 
    dampingRatio: 0.4,
    animations: animation2
)

firstAnimator.addAnimator(secondAnimator, withDelay: 1)
firstAnimator.startAnimation()
Run Code Online (Sandbox Code Playgroud)

我缺少 UIViewPropertyAnimatorGroup 或类似的东西,这甚至存在吗?

nat*_*ter 4

当前推荐的方法是使用 的数组UIViewPropertyAnimator

WWDC 2017 题为“Advanced Animations with UIKit”的演示中详细概述了这种方法。https://developer.apple.com/videos/play/wwdc2017/230/

最重要的一点是将所有动画师放在一起,每当发生基于手势的事件时,您都可以立即对所有动画师应用更新。

var runningAnimators = [UIViewPropertyAnimator]()

// later in the code...

runningAnimators.forEach { $0.pauseAnimation() } // pause all animations
Run Code Online (Sandbox Code Playgroud)

我在 GitHub 上有一个开源存储库,它给出了以这种方式使用多个属性动画器的示例:https ://github.com/nathangitter/interactive-animations


对于您的具体问题,为什么您需要多个属性动画师?通常,当您想要具有多个时序曲线的单个动画时,您只需要多个动画师,但看起来您的时序参数是相同的。