0

我有一个问题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 语句检查),但它只是没有响应。

我在这里做错了什么?

4

1 回答 1

0

根据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()
    }
}
于 2021-03-05T10:58:34.177 回答