我在使用UIViewPropertyAnimator时遇到了一个问题,设置如下:
let animator = UIViewPropertyAnimator(duration: 6.0, curve: .linear)
animator.addAnimations {
UIView.animateKeyframes(withDuration: 6.0, delay: 0.0) {
UIView.addKeyframe(withRelativeStartTime: 0.0, relativeDuration: 0.1) {
someView.alpha = 1.0
}
UIView.addKeyframe(withRelativeStartTime: 0.9, relativeDuration: 0.1) {
someView.alpha = 0.0
}
}
}
@objc func didTapButton {
if animator.isRunning {
animator.isReversed = !animator.isReversed
} else {
print("start")
animator.startAnimation()
}
}当我第一次点击按钮时,动画播放正常。然而,当我第二次点击它时(动画完成后)什么也没有发生。动画制作程序肯定已经停止运行(通过print语句检查),但它就是没有响应。
我在这里做错了什么?
发布于 2021-03-05 18:58:34
根据UIViewPropertyAnimator文档:
When the animator is stopped, either naturally completing or explicitly, any animation blocks and completion handlers are invalidated换句话说,当您第二次调用didTapButton时,您的动画器没有动画。
要解决这个问题,您应该在用户每次点击该按钮时使用addAnimations。
@objc func didTapButton {
if animator.isRunning {
animator.isReversed = !animator.isReversed
} else {
animator.addAnimations {... //insert you animations config ...}
animator.startAnimation()
}
}https://stackoverflow.com/questions/66490680
复制相似问题