如何使用 UIViewPropertyAnimator 为视图层阴影设置动画

Mor*_*hoi 6 animation core-animation ios

我知道我们需要使用CABasicAnimation动画阴影,但我不知道如何UIViewPropertyAnimatorCABasicAnimation.

Mor*_*hoi -2

let shadowAnimation = CABasicAnimation(keyPath: "shadowOpacity")
shadowAnimation.fillMode = kCAFillModeForwards
shadowAnimation.isRemovedOnCompletion = false
shadowAnimation.fromValue = 0.3
shadowAnimation.toValue = 0
shadowAnimation.duration = transitionDuration

animator = UIViewPropertyAnimator(duration: transitionDuration, dampingRatio: 95, animations: {
    topShadowContainer.layer.add(shadowAnimation, forKey: "shadowOpacity")
}
Run Code Online (Sandbox Code Playgroud)

我找到了解决方案。

  • 此代码片段仅执行块中的动画。您无法使用 UIViewPropertyAnimator 的暂停、反转和其他方法。 (10认同)