I have a normal label and want that Label to blink three times, by changing the backgroundcolor from red to black an so on. I did some research and many people work with Timer or animations, but I cannot change this methods, like I need them. What can I do to make my Label blink?
It would be better if you could show more context.I tired this in the viewDidLoad() function,
There's one very simple principle in iOS programming: NEVER call sleep.
(Never call blocking functions in the main thread.)
All the UI updates begins when the current task in the main thread is finished.
UI updates starts after viewDidLoad() is finished, meaning all the intermediate results are not shown and only the last result done in viewDidLoad() will be shown with very long delay.
As you wrote yourself, using Timer is one of the right ways:
Code Block caption.backgroundColor = .black var backgroundColorIsRed = false var blinkCount = 0 Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) {timer in backgroundColorIsRed.toggle() self.caption.backgroundColor = backgroundColorIsRed ? .systemRed : .black blinkCount += 1 if blinkCount >= 4 { timer.invalidate() } }