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
  • 103
  • 1
  • 11

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
  • 447,615
  • 74
  • 748
  • 977
  • 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
  • 16,065
  • 4
  • 61
  • 77