1

我希望能够在我的 Swift iOS 应用程序中有效地使用 UITextView 作为按钮。当我点击文本视图时,我不希望它可编辑,但我希望它改变文本视图的背景颜色并开始使用应用程序中的语音识别软件。我见过其他一些与此类似的问题,但没有人回答这个具体问题。

func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
    guard textView == inView else {

        guard textView == resultView else {
            return false
        }

        nextButton.isEnabled = true

        return false

    }

    if audioEngine.isRunning {

        let myColor: UIColor = UIColor(red: 50, green: 0, blue: 0, alpha: 0.75)

        inView.layer.backgroundColor = myColor.cgColor
        audioEngine.inputNode.removeTap(onBus: 0)

        globalVariables.boolRecording = false
        audioEngine.stop()

        recognitionRequest?.endAudio()
        microphoneButton.isEnabled = true
        microphoneButton.setTitle("Start Recording", for: .normal)

        globalVariables.finalText = globalVariables.finalText + globalVariables.tempText
        globalVariables.tempText = ""

        self.inView.text = ""

        return false

    } else {

        let myColor: UIColor = UIColor(red: 0, green: 50, blue: 0, alpha: 0.75)

        inView.layer.backgroundColor = myColor.cgColor
        globalVariables.boolRecording = true
        startRecording()
        microphoneButton.setTitle("Stop Recording", for: .normal)

        return false

    }

}
4

1 回答 1

4

您可以使用UITextViewDelegate方法:textViewShouldBeginEditing

从此方法返回false以防止将此特定文本字段设为 firstResponder 并更改背景并开始您的语音识别。

func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
    guard textView == speechTextView else {
        return true
    }
    // Change bg
    speechTextView.backgroundColor = UIColor.blue
    // Start speech
    startSpeechRecognition()
    return false
}

不要忘记给自己添加 UITextview 的委托:

speechTextView.delegate = self
于 2018-01-16T16:14:09.443 回答