首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >再次播放UIViewPropertyAnimator时出现问题

再次播放UIViewPropertyAnimator时出现问题
EN

Stack Overflow用户
提问于 2021-03-05 18:16:45
回答 1查看 36关注 0票数 0

我在使用UIViewPropertyAnimator时遇到了一个问题,设置如下:

代码语言:javascript
复制
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语句检查),但它就是没有响应。

我在这里做错了什么?

EN

回答 1

Stack Overflow用户

发布于 2021-03-05 18:58:34

根据UIViewPropertyAnimator文档:

代码语言:javascript
复制
When the animator is stopped, either naturally completing or explicitly, any animation blocks and completion handlers are invalidated

换句话说,当您第二次调用didTapButton时,您的动画器没有动画。

要解决这个问题,您应该在用户每次点击该按钮时使用addAnimations

代码语言:javascript
复制
@objc func didTapButton {
    if animator.isRunning {
        animator.isReversed = !animator.isReversed
    } else {
        animator.addAnimations {... //insert you animations config ...}
        animator.startAnimation()
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66490680

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档