如何在屏幕上显示消息几秒钟?

时间:2023-01-22 23:13:11

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.

在显示消息期间,我宁愿不锁定UI。事实上,如果再次按下按钮,我希望计时器重新启动。我不确定是使用NSTimer,dispatch_after,还是有其他选项。

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?

我目前计划使用NSTimer和UI标签来实现这一目标,而我将只是生活在艰难的消失中。这是最好的方法吗?

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.

编辑:为了澄清,每按一次按钮,消息就不一定相同。我不完全确定这是否相关。

4 个解决方案

#1


6  

Swift 3 Solution:

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 Solution:

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

#2


4  

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

这会在屏幕上显示警报视图,并在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)
    }

#3


3  

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:

在研究了@ mn1评论中的建议之后,我能够完成我想要的。我使用animateWithDuration来淡化标签。这是一些示例代码:

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

#4


1  

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

iOS 10的代码变短了。感谢@fatihyildizhan

fun 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)} )
}

#1


6  

Swift 3 Solution:

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 Solution:

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

#2


4  

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

这会在屏幕上显示警报视图,并在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)
    }

#3


3  

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:

在研究了@ mn1评论中的建议之后,我能够完成我想要的。我使用animateWithDuration来淡化标签。这是一些示例代码:

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

#4


1  

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

iOS 10的代码变短了。感谢@fatihyildizhan

fun 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)} )
}