【问题标题】:Allow Action When Tapping UITextView点击 UITextView 时允许操作
【发布时间】:2018-01-16 15:54:42
【问题描述】:

我希望能够在我的 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

    }

}

【问题讨论】:

    标签: swift uitextview action uitextviewdelegate sfspeechrecognizer


    【解决方案1】:

    您可以使用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
    

    【讨论】:

    • 谢谢——这主要是有效的。但是,我正在尝试使用两个文本视图来执行此操作,而第二个文本视图 resultView 将不起作用,除非它被点击两次。这是为什么?请参阅上面添加的代码。
    • @AEquinox01:你想在两个文本视图上点击相同的操作?
    • 不——我想要两个不同的文本视图有两个不同的操作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-05
    • 2018-01-29
    • 1970-01-01
    • 2017-03-11
    • 2012-01-03
    相关资源
    最近更新 更多