我目前有一个静态背景色:
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/