在Swift中逐渐改变背景颜色

我正在做一个游戏,我需要背景颜色慢慢改变。 当用户玩关卡时,背景颜色应该改变以显示进度。

我想起始颜色是浅蓝色,然后变成绿色,然后是黄色,然后是橙色或类似的东西。

有任何想法吗?

这里有一个工作的例子,只是改变颜色:

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(); } } 

这将循环通过颜色不断,所以你改变了循环mehanism,方法将不断调用自己,直到你发出一个命令来删除所有的animation。

第一:

创build一个视图(或一个框)将其设置为填充屏幕; 让我们称之为背景
将其颜色设置为#e0f1f8(浅蓝色)

下一步:这个代码应该得到一个开始 –

  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 }) 

希望这可以帮助。 祝你好运!

信贷转到:

来源为我的答案