Reputation: 442
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)
Upvotes: 1
Views: 1256
Reputation: 437882
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).
For example:
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
}
})
So, even though the total duration is 2 seconds, the relative start and duration values are percentages, values between 0 and 1.
Upvotes: 3