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?

Nick Pacini
  • 195
  • 2
  • 12
  • This should be what you're looking for http://stackoverflow.com/questions/32211803/swift-background-color-fading-animation-spritekit/32215345#32215345 – 0x141E Aug 29 '15 at 18:47

3 Answers3

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.

Larry Pickles
  • 4,615
  • 2
  • 19
  • 36
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

Community
  • 1
  • 1
FujiRoyale
  • 762
  • 10
  • 26
  • I don't think you can assign color using hex like that. Usually it's a UIColor with an rgb attribute – Dan Beaulieu Aug 29 '15 at 16:02
  • 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. – FujiRoyale Aug 30 '15 at 03:41
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();
                }
        }