1

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!

rmaddy
  • 314,917
  • 42
  • 532
  • 579

1 Answers1

1

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!

  • Ok I did exactly that and now it says "Value of optional type 'String?' not unwrapped; did you mean to use '!' or '?'?" And it is underlining the "t" in the word "text" Please Help! – Sean Bartels Oct 06 '17 at 00:16
  • Oh yeah, change that to: `DispatchTime.now() + (Int(amount.text!)! / 10)`, should work. – Lachlan Walls Oct 07 '17 at 06:09
  • Now, it says. "Binary operator '+' cannot be applied to operands of type 'DispatchTime' and 'Int' " What do I do Now? – Sean Bartels Oct 08 '17 at 22:42
  • Try `DispatchTime.now() + Double(Int(amount.text)! / 10)`, sorry I haven't actually tested it. I'll test it once I get home. – Lachlan Walls Oct 08 '17 at 23:44