How do I make my animation only use colors in the “colors” array and not use oringal white background as it’s first key frame? Please help
Any way to skip/delete/ the first key frame which is white.
var color1 = UIColor.purple
var color2 = UIColor.blue
var color3 = UIColor.green
var color4 = UIColor.yellow
var color5 = UIColor.orange
extension UIViewController {
func viewDidLoad(){
let colors = [color1, color2, color3, color4, color5]
let totalDuration = 5.0
let relDuration: CGFloat = 1.0 / CGFloat(colors.count)
var relStartTime: TimeInterval = 0.0
UIView.animateKeyframes(withDuration: totalDuration, delay: 0.0, options: [.repeat, .allowUserInteraction, .autoreverse, .calculationModeCubicPaced], animations: {
colors.forEach { c in
UIView.addKeyframe(withRelativeStartTime: relStartTime, relativeDuration: relDuration, animations: {
self.view.backgroundColor = c
})
relStartTime = relDuration
}}
)
}
}
CodePudding user response:
It's unclear what you want / expect. Consider: the view background is white, so the first keyframe of your animation animates from that white color to color1
, which is purple. Moreover, you have .autoreverse
, so the last keyframe is the opposite of that, which means we animate from purple to white. Then we start over again, animating from white to purple.
Therefore, during that last frame of the first cycle followed by the first frame of the second cycle, the view background is going to spend, roughly, the last third of the last frame and first third of the first frame of the next cycle, being white.
In other words, there is no "pause". Your animation is "continuous". There is a noticeable white period at the end of each cycle and the start of the next cycle, because that is what you asked for as part of the animation you configured. The animation goes
white
purple
blue
green
yellow
orange [and now reverse]
yellow
green
blue
purple
white
white [and now begin again]
purple
...
If you didn't want to see white for that long, you should not have involved white at the start and end of the animation. It's easy to arrange that, but you need to be more specific in your own mind, and in the question, about what you want the animation to be. When you have decided what the animation should be, you can write that animation, or if you can't, you can ask about it. But at the moment you are simply getting the animation you asked for and you don't seem to have a clear vision of what animation you actually want.
CodePudding user response:
Couldn't figure out a solution in removing/skiping background keyframe so did something else. I changed the orginal backgroundcolor to be my first color in the animation and part of it lol cause I give up.
self.view.backgroundColor = UIColor.purple
and the array just blue, green, yellow, orange, instead of purple being the first one.
I know it's not the best way to approach this but idk any other way.