如何禁用 UIButton 振动

How to disable UIButton vibration

直到今天,我一直在模拟器中测试我的应用程序。我第一次在真实 iPhone 上测试项目。每次我按下按钮时,phone 都会振动,这让我感到不愉快。这没什么大不了的,除非我的特定应用程序有一个内置键盘,每次按下键 (UIButton) 时都会播放音频。

振动声加上音频声音的组合非常分散注意力。 有没有办法以编程方式禁用按钮振动?我不是在问 how to disable all vibration for the phone, just the buttons I choose. The only relevant question I could find was just the opposite: Making the iPhone vibrate

我不必让用户在设置中关闭振动,对吗?

更新 1:

从评论来看,这可能是设备特定问题,或者我以某种方式触发了振动。

这是我播放声音的方式:

import Foundation
import AudioToolbox

class Player {
    
    private let audioFolder = "raw"
    
    func playSoundFromFile(file: String) {
        var soundURL: NSURL?
        var soundID: SystemSoundID = 0
        let filePath = NSBundle.mainBundle().pathForResource("\(audioFolder)/\(file)", ofType: "mp3")
        soundURL = NSURL(fileURLWithPath: filePath!)
        if let url = soundURL {
            AudioServicesCreateSystemSoundID(url, &soundID)
            AudioServicesPlayAlertSound(soundID)
        }
    }
}

这是我的按钮点击操作:

@IBAction func keyTapped(sender: UIButton) {
    
    let buttonText = sender.titleLabel?.text ?? ""
    
    updateDisplayForIpa(buttonText)
    
    // play sound
    if let fileName = singleSound.fileNameForIpa(buttonText) { // converts IPA sound symbol into a filename 
        player.playSoundFromFile(fileName)
    }
    
}

由于tap调用了update display方法,这里是:

func updateDisplayForIpa(ipa: String) {
    
    if let fileName = singleSound.fileNameForIpa(ipa) {
        
        ipaLabel.text = ipa // UILabel
        ipaDescription.text = "\(fileName)_description".localized // UITextView
        ipaDescription.scrollRangeToVisible(NSRange(location: 0, length: 0))
        example1.setTitle("\(fileName)_example1".localized, forState: UIControlState.Normal) // UIButton
        example2.setTitle("\(fileName)_example2".localized, forState: UIControlState.Normal) // UIButton
        example3.setTitle("\(fileName)_example3".localized, forState: UIControlState.Normal) // UIButton
        
    }
}

我在这里没有看到任何会触发振动的东西...

更新 2

我创建了一个新项目,并在情节提要中添加了一个 UIButton。我在上述应用程序中振动的相同 phone 上对其进行了测试。当我点击按钮时没有振动,所以上面的代码一定有什么东西触发了振动。但它会是什么?

AudioServicesPlayAlertSound 会引起振动:

iPhone—plays the specified sound. If the user has configured the Settings application for vibration on ring, also invokes vibration.

我会尝试 AudioServicesPlaySystemSound

To play a short sound not used as an alert, use AudioServicesPlaySystemSound.