I want to set the view's background color and change it to another color after a certain delay. Here is how I tried it:
print("setting color 1")
self.view.backgroundColor = UIColor( rgb: 0xFF0000)
print("sleeping")
sleep(3)
self.view.backgroundColor = UIColor( rgb: 0xFFFF00)
print("setting color 2")
However, I don't get the first color. The app stays at its initial color, waits for 3 seconds and changes than to color 2. No sign of color 1. How to fix that?
sleep(3)
seems to lock the view from updating its color. However, if I call myButton.isEnabled = false
and set it after the delay back to true
, the button behaves as expected and stays disable during the delay.
You can try:
self.view.backgroundColor = UIColor( rgb: 0xFF0000)
DispatchQueue.main.asyncAfter(deadline: .now()+3.0 ) {
self.view.backgroundColor = UIColor( rgb: 0xFFFF00)
}