Gradually change background color in Swift
Asked Answered
P

3

6

I'm making a game where I need the background color to change slowly. As the user plays the level, the background color should be changing to show progress.

I was thinking of having the starting color be light blue, and then changing to green, then yellow, then orange or something like that over time.

Any ideas?

Percutaneous answered 29/8, 2015 at 14:40 Comment(1)
This should be what you're looking for #32212303Protuberate
T
10

Here's a working example, just change the colors:

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

override func viewDidLoad() {
    super.viewDidLoad()
    backgroundColours = [UIColor.redColor(), UIColor.blueColor(), UIColor.yellowColor()]
    backgroundLoop = 0
    self.animateBackgroundColour()
}

func animateBackgroundColour () {
    if backgroundLoop < backgroundColours.count - 1 {
        backgroundLoop++
    } else {
        backgroundLoop = 0
    }
        UIView.animateWithDuration(1, delay: 0, options: UIViewAnimationOptions.AllowUserInteraction, animations: { () -> Void in
            self.view.backgroundColor =  self.backgroundColours[self.backgroundLoop];
        }) {(Bool) -> Void in
            self.animateBackgroundColour();
        }
}

This will cycle through colors endlessly, so you change up the looping mehanism, and the method will continuously call itself until you issue a command to remove all animations.

Thyroid answered 29/8, 2015 at 14:55 Comment(1)
you can use this code wherever you want to, but for this example, I put it in the viewDidLoad of a viewcontrollerThyroid
T
1

first:

Create a view (or a box) set it to fill the screen; lets call it background
set its color to #e0f1f8 (light-blue)

Next: This code should get one started -

      UIView.animateWithDuration(0.5, animations: { () -> Void in
        background.Color = UIColor(red: 238/255.0, green: 238/255.0, blue: 80/255.0, alpha: 1.0)
      })

      UIView.animateWithDuration(0.5, animations: { () -> Void in
        background.Color = UIColor.redColor
      })

Hope this helps. Good luck!

Credit goes to:

Source for my Answer

Tarango answered 29/8, 2015 at 14:55 Comment(2)
I don't think you can assign color using hex like that. Usually it's a UIColor with an rgb attributeDiabolize
Oh. oops :-/ thanks for the pointer there. I do both web and IOS work but lately its been much more web focused so I kinda shorthanded in the wrong direction. I'll make an edit on for that. Thanks.Tarango
D
0

Gradually change background color in Swift: Simply Use

override func viewDidLoad() {
            self.animateBackgroundColour()
        }
func animateBackgroundColour () {
            UIView.animate(withDuration: 10, delay: 0, options: UIView.AnimationOptions.allowUserInteraction, animations: { () -> Void in
                self.YOURVIEW.backgroundColor =  UIColor.random()
                }) {(Bool) -> Void in
                    self.animateBackgroundColour();
                }
        }
Divergency answered 29/9, 2022 at 12:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.