快速动画不会遍历所有关键帧 [英] swift animation does not go through all keyframes

查看:101
本文介绍了快速动画不会遍历所有关键帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过红色->绿色->蓝色,但是当我添加3种颜色时,它只经过了最后两种颜色:

I want to go through red -> green -> blue, but when I add 3 colors, it just go through the last two colors:

    var duration = 6.0

    UIView.animateKeyframesWithDuration(duration, delay: 1.0, options: UIViewKeyframeAnimationOptions.Repeat | UIViewKeyframeAnimationOptions.Autoreverse, animations: { () -> Void in
        self.colorTransition.backgroundColor = UIColor.redColor()
        self.colorTransition.backgroundColor = UIColor.greenColor()
        self.colorTransition.backgroundColor = UIColor.blueColor()

    }, completion: nil)


推荐答案

您必须致电 addKeyframe(withRelativeStartTime:relativeDuration:animations:) 分别指定三个过渡的开始时间(占总持续时间的百分比)以及持续多长时间(

You have to call addKeyframe(withRelativeStartTime:relativeDuration:animations:) for each of the three transitions, specifying when they start (as percentages of the overall duration) and for how long (again, as percentages).

例如:

UIView.animateKeyframes(withDuration: duration, delay: 2, options: [.repeat, .autoreverse], animations: {
    UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 0.33) {
        self.colorTransition.backgroundColor = .red
    }
    UIView.addKeyframe(withRelativeStartTime: 0.33, relativeDuration: 0.33) {
        self.colorTransition.backgroundColor = .green
    }
    UIView.addKeyframe(withRelativeStartTime: 0.66, relativeDuration: 0.34) {
        self.colorTransition.backgroundColor = .blue
    }
})

因此,即使总持续时间为2秒,相对的开始时间和持续时间值也是百分比,值介于0和1之间。

So, even though the total duration is 2 seconds, the relative start and duration values are percentages, values between 0 and 1.

这篇关于快速动画不会遍历所有关键帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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