我想在用户按下按钮后在屏幕上显示消息。然后我希望消息在大约一秒后消失。优选地,它会消失而不是硬消失。
我希望在显示消息时不要锁定UI。事实上,如果再次按下按钮,我希望计时器重新启动。我不确定是使用NSTimer,dispatch_after,还是有其他选项。
我目前计划使用NSTimer和UI标签来实现这一目标,而我只会忍不住消失。这是最好的方法吗?
编辑:为了澄清,每按一次按钮,消息就不一定相同。我不完全确定这是否相关。
答案 0 :(得分:8)
Swift 3解决方案:
// Define a view
var popup:UIView!
func showAlert() {
// customise your view
popup = UIView(frame: CGRect(x: 100, y: 200, width: 200, height: 200))
popup.backgroundColor = UIColor.redColor
// show on screen
self.view.addSubview(popup)
// set the timer
Timer.scheduledTimer(timeInterval: 3.0, target: self, selector: #selector(self.dismissAlert), userInfo: nil, repeats: false)
}
func dismissAlert(){
if popup != nil { // Dismiss the view from here
popup.removeFromSuperview()
}
}
Swift 2解决方案:
// Define a view
var popup:UIView!
func showAlert() {
// customise your view
popup = UIView(frame: CGRect(x: 100, y: 200, width: 200, height: 200))
popup.backgroundColor = UIColor.redColor()
// show on screen
self.view.addSubview(popup)
// set the timer
NSTimer.scheduledTimerWithTimeInterval(3.0, target: self, selector: Selector("dismissAlert"), userInfo: nil, repeats: false)
}
func dismissAlert(){
// Dismiss the view from here
popup.removeFromSuperview()
}
// Don't forget to call showAlert() function in somewhere
答案 1 :(得分:5)
这会在屏幕上显示警报视图,并在1秒后自动关闭。你可以设定时间。
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)
}
答案 2 :(得分:4)
在研究了@ mn1在评论中建议的内容之后,我能够完成我想要的。我使用animateWithDuration来淡化标签。以下是一些示例代码:
myLabel.hidden = false
UIView.animateWithDuration(0.5, animations: { () -> Void in
self.myLabel.alpha = 0
})
答案 3 :(得分:3)
iOS 10中此代码变短了。感谢@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)} )
}
答案 4 :(得分:1)
以下解决方案
myLabel.hidden = false
UIView.animateWithDuration(0.5, animations: { () -> Void in
self.myLabel.alpha = 0
})
重命名为
UIView.animate(withDuration: 5.0, animations: { () -> Void in
self.myLabel.alpha = 0
})
并且不要忘记将viewDidLoad中的表设置为
mylabel.isHidden = true
答案 5 :(得分:1)
对于Swift 5,以下内容是基于上面的出色思想而创建的,下面的内容使用简单的功能创建了一条衰落消息。
首先创建变量,以便稍后调用。
var fadingLabel: UILabel!
按如下所示在viewDidLoad()中创建一个初始隐藏的标签(根据需要调整约束)
// 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
以下功能将显示一个标签,然后在三秒钟内将其慢慢淡出。
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
})
}
例如,使用调用该函数
fadeMessage(message: "Saved", color: .blue, finalAlpha: 0.0)
如果要谨慎保留消息,则将最终的alpha设置为大约0.4;如果希望消失,则将最终的alpha设置为零。