美文网首页SwiftiOS学习OC-开发案例收集
(Swift)延时执行的四种方式

(Swift)延时执行的四种方式

作者: 粒粒皇 | 来源:发表于2018-03-27 10:45 被阅读188次
        // 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()
        }

相关文章

网友评论

    本文标题:(Swift)延时执行的四种方式

    本文链接:https://www.haomeiwen.com/subject/zhujcftx.html