快速背景色动画循环

use*_*658 9 animation loops colors ios swift

我希望我的iOS应用程序的背景颜色在x秒内在四种颜色之间切换

这就是我到目前为止(当我只指定2种颜色时,它正是我想要的)

我还需要动画无限循环运行.

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)

    })
Run Code Online (Sandbox Code Playgroud)

Abh*_*nav 17

试试这个:

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)
                })
        })
}
Run Code Online (Sandbox Code Playgroud)

如果你想要一个连续的重复动画,试试这个:

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)
Run Code Online (Sandbox Code Playgroud)