我翻遍了苹果的iBook,找不到任何关于它的定义:

有人能解释一下dispatch_after的结构吗?

dispatch_after(<#when: dispatch_time_t#>, <#queue: dispatch_queue_t?#>, <#block: dispatch_block_t?#>)

当前回答

以下是Swift中asyncAfter的同步版本:

let deadline = DispatchTime.now() + .seconds(3)
let semaphore = DispatchSemaphore.init(value: 0)
DispatchQueue.global().asyncAfter(deadline: deadline) {
    dispatchPrecondition(condition: .onQueue(DispatchQueue.global()))
    semaphore.signal()
}

semaphore.wait()

与异步的一起:

let deadline = DispatchTime.now() + .seconds(3)
DispatchQueue.main.asyncAfter(deadline: deadline) {
    dispatchPrecondition(condition: .onQueue(DispatchQueue.global()))
}

其他回答

虽然不是OP的原始问题,但某些与NSTimer相关的问题已被标记为该问题的重复,因此值得在这里包含NSTimer的答案。

NSTimer vs dispatch_after

NSTimer更高级,而dispatch_after更低级。 NSTimer更容易取消。取消dispatch_after需要编写更多的代码。

用NSTimer延迟任务

创建一个NSTimer实例。

var timer = NSTimer()

以您需要的延迟启动计时器。

// invalidate the timer if there is any chance that it could have been called before
timer.invalidate()
// delay of 2 seconds
timer = NSTimer.scheduledTimerWithTimeInterval(2.0, target: self, selector: #selector(delayedAction), userInfo: nil, repeats: false) 

添加一个在延迟之后被调用的函数(使用您用于上面选择器参数的任何名称)。

func delayedAction() {
    print("Delayed action has now started."
}

笔记

如果需要在操作发生之前取消该操作,只需调用timer.invalidate()。 对于重复的动作使用repeats: true。 如果你有一个不需要取消的一次性事件,那么就不需要创建计时器实例变量。以下内容就足够了: NSTimer.scheduledTimerWithTimeInterval(2.0, target: self, selector: #selector(delayedAction), userInfo: nil, repeats: false) 在这里看到我更完整的回答。

要在延迟后执行函数或代码,请使用下一个方法

DispatchQueue.main.asyncAfter(deadline: .now() + 'secondsOfDelay') {
        your code here...
    }

在本例中,函数getShowMovies将在1秒后执行

DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
        self.getShowMovies()
    }

我总是喜欢使用扩展而不是自由函数。

斯威夫特4

public extension DispatchQueue {

  private class func delay(delay: TimeInterval, closure: @escaping () -> Void) {
    let when = DispatchTime.now() + delay
    DispatchQueue.main.asyncAfter(deadline: when, execute: closure)
  }

  class func performAction(after seconds: TimeInterval, callBack: @escaping (() -> Void) ) {
    DispatchQueue.delay(delay: seconds) {
      callBack()
    }
  }

}

按以下方法使用。

DispatchQueue.performAction(after: 0.3) {
  // Code Here
}

对于多个函数使用这个。这对于静态函数或任何UI更新使用动画或活动加载器非常有帮助。

DispatchQueue.main.asyncAfter(deadline: .now() + 0.9) {
            // Call your function 1
            DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
                // Call your function 2
            }
        }

例如-在tableView重新加载之前使用动画。或任何其他UI更新后的动画。

*// Start your amination* 
self.startAnimation()
DispatchQueue.main.asyncAfter(deadline: .now() + 0.9) {
                *// The animation will execute depending on the delay time*
                self.stopAnimation()
                DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
                    *// Now update your view*
                     self.fetchData()
                     self.updateUI()
                }
            }

另一种方法是像这样扩展Double:

extension Double {
   var dispatchTime: dispatch_time_t {
       get {
           return dispatch_time(DISPATCH_TIME_NOW,Int64(self * Double(NSEC_PER_SEC)))
       }
   }
}

然后你可以这样使用它:

dispatch_after(Double(2.0).dispatchTime, dispatch_get_main_queue(), { () -> Void in
            self.dismissViewControllerAnimated(true, completion: nil)
    })

我喜欢matt的延迟函数,但只是出于偏好,我宁愿限制传递闭包。