Swift SystemSoundIDButton振动

问题描述 投票:3回答: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

ios iphone swift vibration xcode7.1
1个回答
1
投票

使用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")
 }
}
© www.soinside.com 2019 - 2024. All rights reserved.