【发布时间】:2017-03-09 13:43:37
【问题描述】:
我有一个 UIView 的背景颜色应该改变颜色的时间数组。该数组包含 0-10000ms 的值,其中每个索引是第一次更改和当前更改之间的持续时间。我已经实现了一个循环来在预定的时间执行任务,并且使用打印语句,它似乎正在工作。但是,背景颜色只会变为最后一种颜色,而不是不断变化。
我相信这是因为在循环完成之前背景颜色不会更新。这是否正确,如果正确,我该如何解决?
sendingView.backgroundColor = UIColor.black
let startingTime = getCurrentMillis()
var found = false
for time in receivingMsgData {
while(!found) {
let curDuration = getCurrentMillis() - startingTime
if curDuration > time {
if(sendingView.backgroundColor == UIColor.black) {
sendingView.backgroundColor = UIColor.white
print("playing at \(getCurrentMillis()-startingTime) turning white")
} else {
sendingView.backgroundColor = UIColor.black
print("playing at \(getCurrentMillis()-startingTime) turning black")
}
found = true
}
}
found = false
}
【问题讨论】:
-
在颜色改变后尝试在视图上调用 setNeedsDisplay() 方法。
-
尝试在'UIView.animate(withDuration: ) { }'中编写代码
-
这段代码是在异步还是主线程上执行的?
-
@GirishNair 在主线程上。
-
@JustinChang:检查更新的代码