(Swift) - 延时执行的四种方式
// 1.perform(必须在主线程中执行)
self.perform(#selector(delayExecution), with: nil, afterDelay: 3)
// 取消
NSObject.cancelPreviousPerformRequests(withTarget: self)
// 2.timer(必须在主线程中执行)
Timer.scheduledTimer(timeInterval: 3, target: self, selector: #selector(delayExecution), userInfo: nil, repeats: false)
// 3.Thread (在主线程会卡主界面)
Thread.sleep(forTimeInterval: 3)
self.delayExecution()
// 4.GCD 主线程/子线程
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
self.delayExecution()
}
DispatchQueue.global().asyncAfter(deadline: .now() + 3) {
self.delayExecution()
}
本文由 创作,采用 知识共享署名4.0 国际许可协议进行许可。本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名。最后编辑时间为: 2021/06/07 03:34