swift动画不会遍历所有关键帧

时间:2015-07-07 16:37:01

标签: swift animation uicolor

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

    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)

1 个答案:

答案 0 :(得分:1)

您必须为三个过渡中的每一个调用addKeyframeWithRelativeStartTime,指定它们的开始时间和持续时间。

例如:

UIView.animateKeyframesWithDuration(duration, delay: 1.0, options: .Repeat | .Autoreverse, animations: {
    UIView.addKeyframeWithRelativeStartTime(0, relativeDuration: 0.33) {
        self.colorTransition.backgroundColor = UIColor.redColor()
    }
    UIView.addKeyframeWithRelativeStartTime(0.33, relativeDuration: 0.33) {
        self.colorTransition.backgroundColor = UIColor.greenColor()
    }
    UIView.addKeyframeWithRelativeStartTime(0.66, relativeDuration: 0.34) {
        self.colorTransition.backgroundColor = UIColor.blueColor()
    }
}, completion: nil)