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.
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)
}
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)} )
}
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
})
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With