Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animate UIView background color swift

Tags:

I'd like to animate background of UIView with random colours. here's what i've done so far:

import UIKit

class ViewController: UIViewController {

    var timer = NSTimer()
    var colours = UIColor()

    override func viewDidLoad() {
        super.viewDidLoad()
        getRandomColor()
        timerF()

        UIView.animateWithDuration(2, delay: 0.0, options:[UIViewAnimationOptions.Repeat, UIViewAnimationOptions.Autoreverse], animations: {
            self.view.backgroundColor = self.colours

            }, completion:nil)
    }

    func timerF(){
         timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("getRandomColor"), userInfo: nil, repeats: true)
    }

    func getRandomColor(){
        let red   = Float((arc4random() % 256)) / 255.0
        let green = Float((arc4random() % 256)) / 255.0
        let blue  = Float((arc4random() % 256)) / 255.0
        let alpha = Float(1.0)
        colours = UIColor(colorLiteralRed: red, green: green, blue: blue, alpha: alpha)
    }

}

But it just generates a random colour at the start and use this single colour in my animation. I'd like to find a way to animate colours, so UIVIew background would look like a random rainbow.

like image 585
John White Avatar asked Dec 20 '15 17:12

John White


People also ask

How to set image as UIView background in Swift?

How To Set Image As UIView Background. 3.1 Add Image To The Swift Project. First, you should get a jpg or png format image. Right-click the swift project in Xcode left panel, then click Add Files to “project name” menu item in the popup menu list. Select the image in the top file explorer, and select the project name in Add to targets area.

What is a gradientbackgroundview in Swift?

A common design element in apps is a gradient, often as the background of a view. This post walks through the implementation of a GradientBackgroundView class that: Some examples and implementations of gradient UIView backgrounds in Swift insert a CAGradientLayer at index 0 to a view's layer.

How to add image in Xcode Swift project?

Right-click the swift project in Xcode left panel, then click Add Files to “project name” menu item in the popup menu list. Select the image in the top file explorer, and select the project name in Add to targets area. Then click Add button. Now you can see that the image ( twitter.png in this example ) has been added to the Xcode swift project.

How to move a view from bottom to top in Swift?

The third button ( button text is Bottom To Top UIView Animation ) implements a uiview animation from bottom to top in swift code. When you click the third button, it will first move the view to the bottom, then move the view object to the screen top with animation.


Video Answer


1 Answers

Just put the animation in getRandomColor.

func getRandomColor() {
    let red   = CGFloat((arc4random() % 256)) / 255.0
    let green = CGFloat((arc4random() % 256)) / 255.0
    let blue  = CGFloat((arc4random() % 256)) / 255.0
    let alpha = CGFloat(1.0)

    UIView.animate(withDuration: 1.0, delay: 0.0, options:[.repeat, .autoreverse], animations: {
        self.view.backgroundColor = UIColor(red: red, green: green, blue: blue, alpha: alpha)
    }, completion:nil)
}
like image 119
Caleb Avatar answered Oct 29 '22 21:10

Caleb