I believe this is not a problem of Swift.
It seems AudioServicesPlaySystemSound returns immediately and ignores other calls while playing.
Searching with "ios repeat vibration", I could easily find a Stack Overflow article titled "how to run vibrate continuously in iphone?".
I translated the code into Swift.
In toplevel:
@objc protocol AudioServicesPlaySystemSoundDelegate {
func audioServicesPlaySystemSoundCompleted(soundId: SystemSoundID)
}
func MyAudioServicesSystemSoundCompletionHandler(soundId: SystemSoundID, inClientData: UnsafeMutablePointer<Void>) {
let delegate = unsafeBitCast(inClientData, AudioServicesPlaySystemSoundDelegate.self)
delegate.audioServicesPlaySystemSoundCompleted(soundId)
}
In your ViewController:
class SysSoundViewController: UIViewController, AudioServicesPlaySystemSoundDelegate {
//...
private var repeatCount: Int = 3
@IBAction func repeatVibration(_: AnyObject) {
let proc: AudioServicesSystemSoundCompletionProc = MyAudioServicesSystemSoundCompletionHandler
AudioServicesAddSystemSoundCompletion(kSystemSoundID_Vibrate, nil, nil, proc, UnsafeMutablePointer(unsafeAddressOf(self)))
self.repeatCount = 3
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
}
func audioServicesPlaySystemSoundCompleted(soundId: SystemSoundID) {
if( --repeatCount > 0 ) {
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
}
}
//...
}