1

I use this code inside my motionBegan function. And when i shake my device it vibrates. Is there a way to add a delay so vibration begins after 1 second later for example?

    AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))
Volkan Elçi
  • 163
  • 2
  • 13

2 Answers2

3

Use GCD dispatch_after. (The easiest way is with my delay function, shown here: https://stackoverflow.com/a/24318861/341994.)

Community
  • 1
  • 1
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • 1
    @RamyAlZuhouri Yes it does. I've included a version of the `delay` function adapted for Swift 3. DispatchQueue's `after` is still `dispatch_after` in disguise, anyway! – matt Dec 02 '16 at 16:33
  • @matt is absolutely right. Do note that Apple has unified the dispatch API's to make them more natural in Swift 3. If you're using Swift 3 and up, you should switch to the new `DispatchQueue` syntax I showed in my answer. It does exactly the same thing but it's the right API to use moving forward. – par Dec 02 '16 at 16:48
3

For Swift 3 and up, use a DispatchQueue:

DispatchQueue.main.asyncAfter(.now() + 1.0) {
    AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))
}
par
  • 17,361
  • 4
  • 65
  • 80