Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

    })
like image 802
user3390658 Avatar asked Oct 17 '15 09:10

user3390658


4 Answers

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)
like image 189
Abhinav Avatar answered Nov 13 '22 09:11

Abhinav


Below code helps to keep enable 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)
    }
like image 42
iDevAmit Avatar answered Nov 13 '22 08:11

iDevAmit


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.

like image 20
Arsen Avatar answered Nov 13 '22 10:11

Arsen


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)

}
like image 1
Elshad Karimov Avatar answered Nov 13 '22 08:11

Elshad Karimov