如何推迟在Swift中发生的事情?

Mar*_*Dev 3 ios swift

我需要构建一个iOS Swift单页面应用程序,它具有60:00分钟的倒数计时器.2秒后,我需要显示一个UILabel,在6秒后隐藏它,然后显示另一个文本.到目前为止这是我的代码:

var startTime = NSTimeInterval()
var timer = NSTimer()

func startCountdownTimer() {

    var currentTime = NSDate.timeIntervalSinceReferenceDate()

    //Find the difference between current time and start time.
    var elapsedTime: NSTimeInterval = 3600-(currentTime-startTime)

    //Calculate the minutes in elapsed time.
    let minutes = UInt8(elapsedTime / 60.0)
    elapsedTime -= (NSTimeInterval(minutes) * 60)

    //Calculate the seconds in elapsed time.
    var seconds = UInt8(elapsedTime)
    elapsedTime -= NSTimeInterval(seconds)

    //Add the leading zero for minutes and seconds and store them as string constants
    let strMinutes = minutes > 9 ? String(minutes):"0" + String(minutes)
    let strSeconds = seconds > 9 ? String(seconds):"0" + String(seconds)

    //Concatenate minutes and seconds and assign it to the UILabel
    timerLabel.text = "\(strMinutes):\(strSeconds)"

}
Run Code Online (Sandbox Code Playgroud)

我尝试过这样的事情:

if elapsedTime == 2 {
    introTextLabel.hidden = false
}
Run Code Online (Sandbox Code Playgroud)

或这个:

if (elapsedTime: NSTimeInterval(seconds)) == 2 {
    introTextLabel.hidden = false
}
Run Code Online (Sandbox Code Playgroud)

但它不起作用.有人可以帮忙吗?

introTextLabel - 标签显示文字

timerLabel - 计时器标签

Ces*_*are 9

您可以使用mattdelay()编写的这个有用的功能.

func delay(delay:Double, closure:()->()) {
    dispatch_after(
        dispatch_time(
            DISPATCH_TIME_NOW,
            Int64(delay * Double(NSEC_PER_SEC))
        ),
    dispatch_get_main_queue(), closure)
}
Run Code Online (Sandbox Code Playgroud)

用法:

// Wait two seconds:
delay(2.0) {
    print("Hello!")
}
Run Code Online (Sandbox Code Playgroud)