我希望我的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) })
解决方法
试试这个:
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) }) }) }
如果你想要一个连续的重复动画,试试这个:
UIView.animateWithDuration(2,delay: 0.0,options:[UIViewAnimationOptions.Repeat,UIViewAnimationOptions.Autoreverse],animations: { self.view.backgroundColor = UIColor.blackColor() self.view.backgroundColor = UIColor.greenColor() self.view.backgroundColor = UIColor.grayColor() self.view.backgroundColor = UIColor.redColor() },completion: nil)