Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ios

swift

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.

like image 931
Burgan Avatar asked Nov 22 '15 23:11

Burgan


5 Answers

This shows an Alert View on screen and auto closes after 1 second. You can set the time.

 var alert:UIAlertController!
    func showAlert() {
        self.alert = UIAlertController(title: "Alert", message: "Wait Please!", preferredStyle: UIAlertControllerStyle.Alert)
        self.presentViewController(self.alert, animated: true, completion: nil)
        NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("dismissAlert"), userInfo: nil, repeats: false)
    }

    func dismissAlert(){
        // Dismiss the alert from here
        self.alert.dismissViewControllerAnimated(true, completion: nil)
    }
like image 55
fatihyildizhan Avatar answered Nov 13 '22 01:11

fatihyildizhan


This code became shorter with iOS 10. Thanks to @fatihyildizhan

func showAlert() {
    let alert = UIAlertController(title: "Alert", message: "Wait Please!", preferredStyle: .alert)
    self.present(alert, animated: true, completion: nil)
    Timer.scheduledTimer(withTimeInterval: 3.0, repeats: false, block: { _ in alert.dismiss(animated: true, completion: nil)} )
}
like image 33
AlexSmet Avatar answered Nov 13 '22 00:11

AlexSmet


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
})
like image 8
Burgan Avatar answered Nov 13 '22 01:11

Burgan


the following solution

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

got renamed to

UIView.animate(withDuration: 5.0, animations: { () -> Void in
        self.myLabel.alpha = 0
    })   

and don't forget to set your table in viewDidLoad to

mylabel.isHidden = true
like image 1
Tonkyboy Avatar answered Nov 13 '22 00:11

Tonkyboy


For swift 5, taking from the excellent ideas above, the following creates a fading message using a simple function.

First create variable to call later.

    var fadingLabel: UILabel!

Create an initially hidden label in viewDidLoad() as follows (adjust the constraints as needed)

    // Fading Label
    fadingLabel = UILabel()
    fadingLabel.text = "Text"
    view.addSubview(fadingLabel)
    fadingLabel.isHidden = true

    fadingLabel.translatesAutoresizingMaskIntoConstraints = false
    fadingLabel.topAnchor.constraint    (equalTo: mapView.topAnchor,   constant:     60).isActive = true
    fadingLabel.leftAnchor.constraint   (equalTo: mapView.leftAnchor,  constant:     20).isActive = true
    fadingLabel.widthAnchor.constraint  (equalTo: mapView.widthAnchor, multiplier:  0.2).isActive = true
    fadingLabel.heightAnchor.constraint (equalToConstant: 20                           ).isActive = true

The following function will show a label and then fade it slowly away over three seconds.

func fadeMessage(message: String, color: UIColor, finalAlpha: CGFloat) {
    fadingLabel.text          = message
    fadingLabel.alpha         = 1.0
    fadingLabel.isHidden      = false
    fadingLabel.textAlignment = .center
    fadingLabel.backgroundColor     = color
    fadingLabel.layer.cornerRadius  = 5
    fadingLabel.layer.masksToBounds = true
    fadingLabel.font = appBoldFont(size: 14.0)
    UIView.animate(withDuration: 3.0, animations: { () -> Void in
        self.fadingLabel.alpha = finalAlpha
    })
}

As an example, call the function using,

fadeMessage(message: "Saved", color: .blue, finalAlpha: 0.0)

Set the final alpha to about 0.4 if you want the message to remain discreetly, or to zero if you want it to disappear.

like image 1
Golompse Avatar answered Nov 12 '22 23:11

Golompse