迅速动画不经过所有关键帧(swift animation does not go through a

2019-10-23 16:18发布

我想通过红 - >绿 - >蓝,但是当我添加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)

Answer 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)


文章来源: swift animation does not go through all keyframes