只是简单的实现功能,使用GCD延迟循环调用,主要是swift对gcd写法的改变,可以写个单例,全局调用,方法内部可以自定义系统声音等,这就不写了,只有震动效果,直接上代码:
//开始持续震动
func startSystemVibrate() {
AudioServicesAddSystemSoundCompletion(kSystemSoundID_Vibrate, nil, nil, { (sound,_) in
let additionalTime: DispatchTimeInterval = .seconds(1)
DispatchQueue.main.asyncAfter(deadline: .now() + additionalTime, execute: {
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
})
}, nil);
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}
//结束持续震动
func stopSystemVibrate() {
AudioServicesRemoveSystemSoundCompletion(kSystemSoundID_Vibrate);
AudioServicesDisposeSystemSoundID(kSystemSoundID_Vibrate);
}