user3390658
user3390658

Reputation: 277

swift background color animation loop

I want the background color of my iOS app to change between four colors over x amount of seconds

This is what I have so far (it does exactly what I want when I specify just 2 colors)

I also need the animation to run in a loop infinitely.

ViewController.swift

    UIView.animateWithDuration(X.0, animations: {
        // Color 1
        self.view.backgroundColor = UIColor(rgba)
        // Color 2
        self.view.backgroundColor = UIColor(rgba)
        // Color 3
        self.view.backgroundColor = UIColor(rgba)
        // Color 4
        self.view.backgroundColor = UIColor(rgba)

    })

Upvotes: 11

Views: 19057

Answers (5)

iDevAmit
iDevAmit

Reputation: 1578

Below code helps enable the user interaction with animated view. A random color generation (use in case its needed).

UIView.animateWithDuration(7, delay: 1, options: [UIViewAnimationOptions.AllowUserInteraction,
UIViewAnimationOptions.Repeat,
UIViewAnimationOptions.Autoreverse],
animations: {
               self.yourView.backgroundColor = self.randomColor()
               self.yourView.backgroundColor = self.randomColor()
            }, completion:nil)

func randomColor() -> UIColor {
   let randomRed:CGFloat = CGFloat(drand48())
   let randomGreen:CGFloat = CGFloat(drand48())
   let randomBlue:CGFloat = CGFloat(drand48())
   return UIColor(red: randomRed, green: randomGreen, blue: randomBlue, alpha: 1.0)
}

Upvotes: 3

serenn
serenn

Reputation: 1828

i had a dynamic / unknown amount of colors to cycle through, so i refactored the Swift 5 answer a bit:

Swift 5

            UIView.animate(withDuration: 1.0, delay: 0.0, options: [.repeat, .autoreverse]) {
                // colors is the dynamic [UIColor] array previously defined
                for color in colors {
                    self.colorView.backgroundColor = color
                }
            } completion: { (finished) in
                
            }

this will nicely cycle through the colors with a fade-style animation -- and with the .autoreverse option, it won't "snap" to the starting color.

Upvotes: 0

Elshad Karimov
Elshad Karimov

Reputation: 322

Swift 5

override func viewWillAppear(_ animated: Bool) {
    self.customButton.backgroundColor = .red
    UIView.animateKeyframes(withDuration: 0.5, delay: 0, options: [.repeat, .autoreverse], animations: {
        self.customButton.backgroundColor = .none
    }, completion: nil)

}

Upvotes: 1

Abhinav
Abhinav

Reputation: 38162

Try this out:

UIView.animateWithDuration(1.0, animations: { () -> Void in
    self.view.backgroundColor = UIColor.blackColor()
    }) { (Bool) -> Void in
        UIView.animateWithDuration(1.0, animations: { () -> Void in
            self.view.backgroundColor = UIColor.greenColor()
            }, completion: { (Bool) -> Void in
                UIView.animateWithDuration(1.0, animations: { () -> Void in
                    self.view.backgroundColor = UIColor.grayColor()
                    }, completion: { (Bool) -> Void in
                        UIView.animateWithDuration(1.0, animations: { () -> Void in
                            self.view.backgroundColor = UIColor.redColor()
                            }, completion:nil)
                })
        })
}

In case you want a continuous repeating animation, try this out:

UIView.animate(withDuration: 2, delay: 0.0, options:[UIView.AnimationOptions.repeat, UIView.AnimationOptions.autoreverse], animations: {
     self.view.backgroundColor = UIColor.black
     self.view.backgroundColor = UIColor.green
     self.view.backgroundColor = UIColor.darkGray
     self.view.backgroundColor = UIColor.red
}, completion: nil)

Upvotes: 19

Arsen
Arsen

Reputation: 10961

You have to use NSTimer:

let timer = NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: "update", userInfo: nil, repeats: true)

func update() {
    let nextCollor = getNextColor()
    UIView.animateWithDuration(X.0, animations: {
        self.view.backgroundColor = nextCollor
    })
}

func getNextColor() -> UIColor {
    let currentColor = self.view.backgroundColor

    if currentColor == smaple1 {
        return UIColor.redColor()
    } else if currentColor == smaple2 {
        return UIColor.grayColor()
    } else {
        return UIColor.whiteColor()
    }
}

NSTimer.scheduledTimerWithTimeInterval runs your code every 5 seconds

PS: do not forget invalidate timer when you done with it. Just call timer.invalidate() for it. Otherwise you get a crash.

Upvotes: 2

Related Questions