【问题标题】:Know when dictation has ended in a UITextView知道听写何时在 UITextView 中结束
【发布时间】:2015-02-27 10:17:20
【问题描述】:

我想知道听写什么时候结束(最好是什么时候开始)。

我的UIViewController 包含UITextView 符合UITextInputDelegate 协议。

为了让它工作,我必须订阅UITextInputCurrentInputModeDidChangeNotification

override func viewDidLoad() {
    super.viewDidLoad()
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "changeInputMode:", name: UITextInputCurrentInputModeDidChangeNotification, object: nil)
}

并在那里添加委托(仅将其添加到 viewDidLoad() 是行不通的)

func changeInputMode(sender : NSNotification) {
    textView.inputDelegate = self
}

现在开始和停止听写 UITextInput 可以正确调用所需的委托方法:

func selectionWillChange(textInput: UITextInput){    }
func selectionDidChange(textInput: UITextInput){    }
func textWillChange(textInput: UITextInput){    }
func textDidChange(textInput: UITextInput){    }

但是没有被调用的是

func dictationRecordingDidEnd() {
    println("UITextInput Dictation ended")
}

为什么?如何在听写结束时收到通知/调用方法?

【问题讨论】:

    标签: ios swift uitextview dictation


    【解决方案1】:

    好的,这就是我不使用UITextInput 协议而是使用UITextInputCurrentInputModeDidChangeNotification 的方法。

    func changeInputMode(sender : NSNotification) {
        var primaryLanguage = textView.textInputMode?.primaryLanguage
    
        if primaryLanguage != nil {
            var activeLocale:NSLocale = NSLocale(localeIdentifier: primaryLanguage!)
            if primaryLanguage == "dictation" {
                // dictation started
            } else {
                // dictation ended
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-07
      • 1970-01-01
      • 2013-03-19
      • 1970-01-01
      • 1970-01-01
      • 2020-10-15
      • 2016-04-17
      • 1970-01-01
      相关资源
      最近更新 更多