J'ai un problème avec la fonction sleep
en code Swift. J'utilise import Darwin
et usleep(400000)
. Certaines actions avant d'atteindre le sommeil sont bloquées et je ne sais pas pourquoi. Voici un court exemple de mon code:retard/sommeil dans Swift ne fonctionne pas
@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()
}
Ces lignes ne seront pas exécutées:
Antwort4.backgroundColor = UIColor.green
Ende.text = "Richtig!"
Pourquoi appeler le sommeil le blocage de ces actions? Si je supprime le import Darwin
et le sleep
, mon code fonctionne correctement. Quelqu'un a-t-il une idée? Désolé pour mon mauvais anglais: P
N'utilisez pas 'sleep' ou des variantes. Vous bloquez le fil principal en ne faisant rien. Utilisez ['dispatch_after'] (https://developer.apple.com/reference/dispatch/1452876-dispatch_after) – jcaron