【发布时间】:2017-07-16 15:33:56
【问题描述】:
我对 Swift 代码中的 sleep 函数有疑问。我正在使用import Darwin 和usleep(400000)。进入睡眠之前的一些动作被阻止,我不知道为什么。这是我的代码中的一个简短示例:
@IBAction func Antwort4Button(_ sender: Any) {
if (richtigeAntwort == "4"){
Antwort4.backgroundColor = UIColor.green
Ende.text = "Richtig!"
NaechsteFrage()
}
else {
Ende.text = "Falsch!"
//NaechsteFrage()
}
}
func NaechsteFrage() {
usleep(400000)
Antwort1.backgroundColor = UIColor.red
Antwort2.backgroundColor = UIColor.red
Antwort3.backgroundColor = UIColor.red
Antwort4.backgroundColor = UIColor.red
Ende.text = ""
FragenSammlung()
}
这些行将不会被执行:
Antwort4.backgroundColor = UIColor.green
Ende.text = "Richtig!"
为什么调用 sleep 会阻止这些操作?如果我删除import Darwin 和sleep,我的代码可以正常工作。有人有想法吗?对不起我的英语不好:P
【问题讨论】:
-
不要使用
sleep或变体。你阻塞了主线程什么都不做。使用dispatch_after
标签: ios swift delay wait sleep