Swift SystemSoundIDButton Vibrate

时间:2015-11-01 22:36:12

标签: ios iphone swift vibrate xcode7.1

我有这段代码

    import AudioToolbox.AudioServices

    if restoreData.boolForKey("VibrationSwitchButton") == true {
        vibra()
    }

    func vibra() {
        if UIDevice.currentDevice().model == "iPhone" {
        let systemSoundIDButton : SystemSoundID = 4095
        AudioServicesPlayAlertSound(SystemSoundID(systemSoundIDButton))
        // print("VIBRA")}
    }

我有一个UISwitch,可以激活和停用这段代码。开关工作正常,但如果我在常规选项(声音)中激活振动,我无法从我的设置中取消激活,反之亦然。

如何概括我的配置?如果我在一般设置中振动,我可以在我的应用程序上停用

swift 2.0 xcode 7.1

1 个答案:

答案 0 :(得分:0)

使用AudioServicesPlaySystemSound代替AudioServicesPlayAlertSound

示例代码:

if restoreData.boolForKey("VibrationSwitchButton") == true {
   vibra()
} else {
   soundOnly()
}

func soundOnly() {
 if UIDevice.currentDevice().model == "iPhone" {
    let systemSoundIDButton : SystemSoundID = 4095
    AudioServicesPlaySystemSound(SystemSoundID(systemSoundIDButton))
     // print("Sound Only")
 }
}