美文网首页
Swift: 延时执行

Swift: 延时执行

作者: ChaosHeart | 来源:发表于2021-10-15 10:57 被阅读0次
    // 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()
    }
    //5.GCD全局
    DispatchQueue.global().asyncAfter(deadline: .now() + 3) {
        self.delayExecution()
    }
    

    相关文章

      网友评论

          本文标题:Swift: 延时执行

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