Search code examples
iosswiftswift4.1

how to delay a execution?


I am creating a small project such that after pressing a button which will take me to the next view controller using the navigation controller - can i delay the function to execute the navigation controller after some time

i am using this code-

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()
   }

}

Solution

  • Suppose sender.tag holds the duration which is 0 for proceed , 5 or 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
    
        })
    }