Add a delay to a for loop in swift

后端 未结 6 1577
感动是毒
感动是毒 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条回答
  •  时光取名叫无心
    2021-01-05 08:53

    Marked answer doesn't delay loop iterations and you still get just the last value in the label.text.

    You can solve it like this:

    func showWelcome(_ iteration: Int = 0) {
        let i = iteration>=self.welcomeContainer.count ? 0 : iteration
        let message = self.welcomeContainer[i]
        self.delay(2){
            self.welcomeLabel.text = message
            return self.showWelcome(i + 1)
        }
    }
    

    Usage:

        showWelcome()
    

提交回复
热议问题