如何延迟执行?

时间:2018-06-27 11:39:16

标签: ios swift swift4.1

我正在创建一个小项目,以便在按下按钮后 会带我使用导航控制器进入下一个视图控制器-我可以在一段时间后延迟执行导航控制器的功能

我正在使用此代码-

import UIKit

class ViewController: UIViewController
{
   @IBAction func enterButtonPressed(_ sender: UIButton)
   {
       let vc = storyboard?.instantiateViewController(withIdentifier: "ViewController2") as! ViewController2
       self.navigationController?.pushViewController(vc, animated: true)
   }

   override func viewDidLoad()
   {
      super.viewDidLoad()
   }

   override func didReceiveMemoryWarning() 
   {
      super.didReceiveMemoryWarning()
   }

}

2 个答案:

答案 0 :(得分:2)

假设sender.tag的持续时间为proceed的持续时间为0或5或10

var playTimer:Timer?

//

@IBAction func btnClicked(_ sender:UIButton) {

    playTimer?.invalidate()

    playTimer = Timer.scheduledTimer(withTimeInterval: TimeInterval(sender.tag), repeats: false, block: { (T) in

        // play the audio

    })
}

答案 1 :(得分:0)

尝试这个

var timeLimit = 5.0
DispatchQueue.main.asyncAfter(deadline: .now() + timeLimit, execute: {
    //Play your audio here
    //Audio will play after 5 seconds here
})