Add a delay to a for loop in swift

后端 未结 6 1584
感动是毒
感动是毒 2021-01-05 08:02

I have a coding \'issue\'.

I have a label, which text I want to change dynamically every 2 seconds. I\'ve done the following:

// WELCOME STRING ARRA         


        
6条回答
  •  梦毁少年i
    2021-01-05 08:35

    You can also use this function to delay something

    //MARK: Delay func 
    
    func delay(_ delay:Double, closure:@escaping ()->()) {
        DispatchQueue.main.asyncAfter(
            deadline: DispatchTime.now() + Double(Int64(delay * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC), execute: closure)
    }
    

    and usage is :

            delay(2)  //Here you put time you want to delay
    {
                //your delayed code
            }
    

    Hope it will help you.

提交回复
热议问题