-1

I'm trying to rotate the background colour endlessly between two different gradients, but the actual transition isn't lasting as long as expected (it lasts for less than a second and there's no delay between each transition).

var backgroundColours = [CAGradientLayer()]
var backgroundLoop = 0

override func viewDidLoad() {
    super.viewDidLoad()
    backgroundColours = [Colors.init().one, Colors.init().two] // These are two CAGradientLayers but I haven't included their code
    backgroundLoop = 0
    self.animateBackgroundColour()
}

func animateBackgroundColour () {

    backgroundLoop = (backgroundLoop + 1) % (backgroundColours.count)

    UIView.animate(withDuration: 2.0, delay: 2.0, options: .curveLinear, animations: {
        self.view.backgroundColor = UIColor.clear
        let backgroundLayer = self.backgroundColours[self.backgroundLoop]
        backgroundLayer.frame = self.view.frame
        self.view.layer.insertSublayer(backgroundLayer, at: 0)
    }, completion: { (Bool) -> Void in
        self.animateBackgroundColour()
    })

This is all inside the ViewController Class

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Adi219
  • 4,712
  • 2
  • 20
  • 43

1 Answers1

0

UIView.animate animates only about five specialized view properties. You are not animating any of those properties. What you want is layer animation (e.g. CABasicAnimation).

matt
  • 515,959
  • 87
  • 875
  • 1,141