How do I add label values to wait commands?

60 Views Asked by At

I am making an app that vibrates with customizable breaks in between. I have a slider that is numbered 0-50. And it rounds it to the closest integer then displays it in my label. I am now trying to execute the command:

@IBOutlet weak var amount: UILabel!

@IBAction func slider1(_ sender: UISlider) {

    amount.text = String(Int(sender.value));

}

@IBAction func vibrator(_ sender: UISlider) {

   AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)

    (the wait goes here but I don't know how to add it)
}

also if possible in the wait command i want to divide the label value by 10. it doesn't have to be specifically the wait command, just something to delay it.

Thanks in advance!

1

There are 1 best solutions below

4
Lachlan Walls On

This is the best way to do this:

DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1.0) {
    // this runs after a 1.0 second wait.
}

In this example, the code inside the function runs after an amount of time declared here:

DispatchTime.now() + 1.0

The 1.0 is the time in seconds to wait, essentially. I'm assuming from what you said, you'll want to change that bit of code to:

DispatchTime.now() + Double(Int(amount.text)! / 10)

I hope this is the answer you were looking for!