简体   繁体   English

如何禁用UIButton振动

[英]How to disable UIButton vibration

Up until today I have been testing my apps in the Simulator. 直到今天,我一直在模拟器中测试我的应用程序。 For the first time I tested a project on a real iPhone. 我第一次在真实的iPhone上测试了一个项目。 I was unpleasantly surprised that every time I press a button the phone vibrates. 每当我按下按钮时,手机都会震动,这让我感到不愉快。 This wouldn't be a big deal except that my particular app has a built in keyboard that plays an audio sound every time a key ( UIButton ) is pressed. 没什么大不了的,除了我的特定应用程序具有内置的键盘,该键盘在每次按下键( UIButton )时都会播放音频。

在此处输入图片说明

The combination of the vibration sound plus the audio sound is very distracting. 振动声音和音频声音的组合非常令人分心。 Is there any way to programmatically disable button vibration? 有什么办法以编程方式禁用按钮振动? I'm not asking 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 . 我能找到的唯一相关问题恰恰相反: 使iPhone振动

I don't have to leave it up to the user to turn vibration off in settings, do I? 我不必任由用户关闭设置中的振动,对吗?

Update 1: 更新1:

Judging from the comments, maybe this is either a device specific problem or I am somehow triggering a vibration. 从评论中可以看出,这可能是设备特定的问题,还是我以某种方式触发了振动。

This is how I play a sound: 这是我播放声音的方式:

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)
        }
    }
}

And this is my button tap action: 这是我的按钮点击动作:

@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)
    }

}

Since the tap calls an update display method, here that is: 由于点击会调用更新显示方法,因此这里是:

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

    }
}

I don't see anything here that would trigger a vibration... 我在这里看不到会引起振动的任何东西...

Update 2 更新2

I created a new project and added a single UIButton to the storyboard. 我创建了一个新项目,并将一个UIButton添加到情节提要。 I tested it on the same phone that vibrates in the above app. 我在与上述应用程序相同的手机上对其进行了测试。 There was no vibration when I tapped the button, so there must be something about the above code that is triggering a vibration. 当我点击按钮时没有震动,因此上面的代码中一定有一些东西会触发震动。 But what could it be? 但是那会是什么呢?

AudioServicesPlayAlertSound can cause a vibration: AudioServicesPlayAlertSound会引起振动:

iPhone—plays the specified sound. iPhone-播放指定的声音。 If the user has configured the Settings application for vibration on ring, also invokes vibration. 如果用户已将“设置”应用程序配置为振铃振动,则还会调用振动。

I'd try AudioServicesPlaySystemSound instead: 我会改用AudioServicesPlaySystemSound

To play a short sound not used as an alert, use AudioServicesPlaySystemSound . 要播放不用作警报的短声音,请使用AudioServicesPlaySystemSound

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM