使用UIViewPropertyAnimator多次重复和反转动画 [英] Repeating and reversing an animation multiple times using UIViewPropertyAnimator

查看:374
本文介绍了使用UIViewPropertyAnimator多次重复和反转动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让我的动画再次使屏幕从黑色变为白色再变为黑色,并重复一定次数.目前,使用该代码,我可以使动画从黑色过渡到白色,然后又跳回黑色.反过来说,是否可以反向运行动画或添加在第一个动画完成后运行的动画?

I am trying to have my animation ease the screen from black to white to black again and repeat that a certain amount of times. Currently with the code I have the animation eases from black to white then jumps back to black. Is there anyway to run an animation in reverse or add an animation that runs after the first animation is completed?

override func viewDidAppear(_ animated: Bool) {
    let viewColorAnimator: UIViewPropertyAnimator = UIViewPropertyAnimator.runningPropertyAnimator(
        withDuration: 4.0,
        delay: 0.0,
        options: [.curveEaseInOut],
        animations: {
            UIView.setAnimationRepeatCount(3);
            self.lightView.backgroundColor = .white
        })

    viewColorAnimator.startAnimation()
}

我尝试将这段代码添加到项目中,但结果是相同的:

I tried adding this block of code to the project but the outcome was the same:

viewColorAnimator.addCompletion {_ in
    let secondAnimator = UIViewPropertyAnimator(duration: 4.0, curve: .linear) {
        self.lightView.backgroundColor = .black
    }
    secondAnimator.startAnimation()
}

我发现这是由于setAnimationRepeatCount所致,因为最后的迭代工作正常.如何多次运行动画而没有重复计数?

I've found out it is because of the setAnimationRepeatCount because the last of the iterations works properly. How do I run the animation multiple times without the repeat count?

推荐答案

文档说与方向相关的选项被忽略.您可以在此处检查图像.

Documentation says that the options related to direction are ignored. you can check the image attached here.

要实现反向动画:

创建一个动画师属性.

private var animator: UIViewPropertyAnimator = {

    let propertyAnimator = UIViewPropertyAnimator.runningPropertyAnimator(
    withDuration: 4.0,
    delay: 0.0,
    options: [.curveEaseInOut, .autoreverse],
    animations: {
        UIView.setAnimationRepeatCount(3)
        UIView.setAnimationRepeatAutoreverses(true)
        self.lightView.backgroundColor = .white
    })

    return propertyAnimator
}()

P.S.我们需要在选项中提及.autoreverse.否则,UIView.setAnimationRepeatAutoreverses(true)将无法工作.

P.S. we need to mention the .autoreverse in the options. Otherwise UIView.setAnimationRepeatAutoreverses(true) won't work.

这篇关于使用UIViewPropertyAnimator多次重复和反转动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆