Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I animate my View Controller's background to fade among multiple colors?

How can I do that without causing infinite memory issues? I'm trying something like this:

override func viewDidAppear(animated: Bool) {
        view.backgroundColor = UIColor.whiteColor()
        fadeBackground()
    }

    let colors = [UIColor.blueColor(), UIColor.redColor(), UIColor.greenColor(), UIColor.orangeColor(), UIColor.purpleColor()]
    func fadeBackground(){
        UIView.animateWithDuration(4, delay: 0, options: UIViewAnimationOptions.CurveLinear, animations: {
            var randomIndex = Int(arc4random_uniform(UInt32(self.colors.count)))
            self.view.backgroundColor = self.colors[randomIndex]
        }) { (stuff Bool) -> Void in
            self.fadeBackground()
        }
    }

I'm afraid this will eventually eat up the memory. How can I do this without recursion? Do I need [unowned self] ?

like image 414
TIMEX Avatar asked Dec 29 '25 02:12

TIMEX


1 Answers

I believe that's the right code. Try setting view.backgroundColor to an initial color like UIColor.whiteColor() and try again. Then you can randomly pick another color with arc4random() or something and call the same method.

like image 111
Schemetrical Avatar answered Dec 30 '25 16:12

Schemetrical



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!