ios - 在 Swift 3 中设置动画背景颜色

标签 ios swift uiviewanimation

我目前有一个静态背景色:

self.backgroundColor = UIColor(red: 0.0, green: 0.0, blue: 1.0, alpha: 1.0)

我如何编写代码以在设定的秒数(例如每种颜色 5 秒)内创建一系列动画颜色,然后返回到颜色循环的开头?

我尝试了以下...

self.backgroundColor = UIColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0)
UIView.animate(withDuration: 5.0, animations: { () -> Void in
    self.backgroundColor = UIColor(red: 0.0, green: 1.0, blue: 0.0, alpha: 1.0);
    self.backgroundColor = UIColor(red: 0.0, green: 0.0, blue: 1.0, alpha: 1.0)
})

...但这只是停留在最后一种蓝色。

最佳答案

必须制作嵌套动画

    self.backgroundColor = UIColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0)
    UIView.animate(withDuration: 5.0, animations: { () -> Void in
        self.backgroundColor = UIColor(red: 0.0, green: 1.0, blue: 0.0, alpha: 1.0);
    },
     completion: { finished in
        UIView.animate(withDuration: 5.0, animations: { () -> Void in
            self.backgroundColor = UIColor(red: 0.0, green: 0.0, blue: 1.0, alpha: 1.0)
        }, completion: { finished in
            //other color do the same
        })

    })

关于ios - 在 Swift 3 中设置动画背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45170874/

相关文章:

ios - 在没有 mac 的情况下查看 iphone 控制台日志

快速构建不起作用

objective-c - 从 super View 中删除并启用用户交互

iphone - 不使用 NavigationController 的 UIViewController 的 Push-Pop 类动画

iphone - CAReplicatorLayer 随机位置

iOS Parse.com 使用指针列保存对象会出现 "user not allowed"错误

ios - Card.IO自定义手动画面

swift - 为什么我无法从 Parse 检索对象并将其存储在数组中?

swift - 在 Swift 3 中总结时间间隔和日期时出现奇怪的问题

ios - 在动画 block 中调用时,UIView 动画无法无限重复