Volkan Elçi
Volkan Elçi

Reputation: 164

Swift - How to add a delay for vibrate

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))

Upvotes: 1

Views: 950

Answers (2)

par
par

Reputation: 17724

For Swift 3 and up, use a DispatchQueue:

DispatchQueue.main.asyncAfter(.now() + 1.0) {
    AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))
}

Upvotes: 3

matt
matt

Reputation: 535402

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

Upvotes: 3

Related Questions