找到你要的答案

Q:Making All App Vibrations Stop Setting

Q:使所有的应用程序振动停止设置

I am trying to create a button within my app that turns of the vibrations that occur when some buttons are pressed within the app.

I have used the:

AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))

code to enable the vibrate on certain button presses - but I want the user to be able to turn off the vibration, if they so wanted.

Is there a way to disable all vibrations from a single .disable or .invalidate type of way (those examples are the only code cancelling things I could think of)

我试图在我的应用程序中创建一个按钮,当应用程序中的一些按钮被按下时会产生振动。

我用过:

audioservicesplaysystemsound(systemsoundid(ksystemsoundid_vibrate))

代码,以使某些按钮印刷机振动-但我希望用户能够关闭振动,如果他们想。

有没有一种方法来禁用所有振动从单一。禁用或。无效的方式(这些例子是唯一的代码取消的事情我能想到的)

answer1: 回答1:

You can create one bool and store it's state to NSUserDefaults then add a if condition for vibration like this:

if allowVibration {

    AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
}

And it will not enter into this if condition if allowVibration is false.

你可以创建一个bool和存储的状态nsuserdefaults然后添加如果振动这样的条件:

if allowVibration {

    audioservicesplaysystemsound(systemsoundid(ksystemsoundid_vibrate))
}

它不会进入这个条件如果allowvibration是假的。

answer2: 回答2:

Use NSUserDefaults to store a bool that keeps track of whether or not to play the vibration. If you don't want to keep track of this between application sessions just use a bool.

// Get NSUserDefaults
let defaults = NSUserDefaults.standardUserDefaults()

// Some button that would turn off vibration
@IBAction func turnVibrateOff(sender: AnyObject) {
    defaults.setBool(false, forKey: "vibrate")
    NSUserDefaults.standardUserDefaults().synchronize()
}

// Some button that would turn on vibration
@IBAction func turnVibrateOn(sender: AnyObject) {
    defaults.setBool(true, forKey: "vibrate")
    NSUserDefaults.standardUserDefaults().synchronize()
}

// Then just check against the key we've created
func shouldWeVibrate() {
    if defaults.boolForKey("vibrate") {
        // Vibrate
    }
}

使用nsuserdefaults存储bool记录是否发挥振动。如果你不想了解这个应用程序会话间只使用一个布尔。

// Get NSUserDefaults
let defaults = NSUserDefaults.standardUserDefaults()

// Some button that would turn off vibration
@IBAction func turnVibrateOff(sender: AnyObject) {
    defaults.setBool(false, forKey: "vibrate")
    NSUserDefaults.standardUserDefaults().synchronize()
}

// Some button that would turn on vibration
@IBAction func turnVibrateOn(sender: AnyObject) {
    defaults.setBool(true, forKey: "vibrate")
    NSUserDefaults.standardUserDefaults().synchronize()
}

// Then just check against the key we've created
func shouldWeVibrate() {
    if defaults.boolForKey("vibrate") {
        // Vibrate
    }
}
ios  swift  vibrate