简体   繁体   中英

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

For Swift 3 and up, use a DispatchQueue :

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM