FlashDrive FlashDrive - 24 days ago 14
iOS Question

How to show a message on screen for a few seconds?

I want to display a message on screen after the user presses a button. I then want the message to disappear after about a second. Preferably it would fade away instead of a hard disappear.

I would rather not lock up the UI during the display of the message. In fact, I would like the timer to restart for the message if the button is pressed again. I'm unsure of whether to use NSTimer, dispatch_after, or if there are other options.

I currently plan to use an NSTimer and a UI label to achieve this, and I will just live with a hard disappear. Is that the best way to do it?

EDIT: To clarify, the message will not necessarily be the same every single time that the button is pushed. I'm not entirely sure if this is relevant though.

Answer

I was able to accomplish what I wanted after researching what was suggested in the comment by @mn1. I used animateWithDuration to fade the label away. Here is some example code:

myLabel.hidden = false
UIView.animateWithDuration(0.5, animations: { () -> Void in
    self.myLabel.alpha = 0
})