我有一个运行完美的简单脉冲动画
UIView.animate(withDuration: 1, animations: {
myAnimatableView.transform = CGAffineTransform(scaleX: 1.2, y: 1.2)
}) { _ in
UIView.animate(withDuration: 1, animations: {
myAnimatableView.transform = .identity
})
}
Run Code Online (Sandbox Code Playgroud)
我想制作相同的动画,但现在使用UIViewPropertyAnimator. 是否可以使用新 API 优雅地复制这种脉冲效果?
我是这样做的
let animator = UIViewPropertyAnimator(duration: 1, curve: .linear) {
animatableView.transform = CGAffineTransform(scaleX: 1.2, y: 1.2)
}
animator.addCompletion { _ in
animator.isReversed = true
animator.startAnimation()
}
animator.startAnimation()
Run Code Online (Sandbox Code Playgroud)
但我animatableView只是卡在缩放到 1.2。
似乎属性动画师在完成后删除动画(这可以解释再次启动它没有效果) - 请参阅SO question。基于此,您可能会尝试破解它pausesOnCompletion,但我希望这不会像您想象的那么容易。仅仅因为文档说:
当此属性的值为 true 时,动画完成时动画器保持活动状态,并且不执行其完成处理程序。
这意味着如果pauseOnCompletion设置为 true ,则不会调用完成处理程序。根据相同的文档,您可以通过观察isRunning动画师的属性来确定动画师何时结束(以及何时应该重新启动动画)。但是,当我尝试实现它时,我发现isRunning在动画师的生命周期中不会调用KVO on 。因此我最后的建议是简单地使用两个动画师,因为它似乎更容易实现:
let animator = UIViewPropertyAnimator(duration: 1, curve: .linear) {
animatableView.transform = CGAffineTransform(scaleX: 1.2, y: 1.2)
}
animator.addCompletion { _ in
let secondAnimator = UIViewPropertyAnimator(duration: 1, curve: .linear) {
animatableView.transform = CGAffineTransform.identity
}
secondAnimator.startAnimation()
}
animator.startAnimation()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6706 次 |
| 最近记录: |