2014-11-02 147 views
0

我正在用iOS上的新语言swift测试键盘扩展,但我无法播放keyPressed事件的声音,并且存在延迟,大约10秒钟,当我单击键。iOS快速自定义键盘没有声音和延迟

这是我的代码:

@IBAction func keyPressed(button: UIButton) { 
    var string = button.titleLabel!.text 
    (textDocumentProxy as UIKeyInput).insertText("\(string!)") 
    AudioServicesDisposeSystemSoundID(1104) 
    AudioServicesPlaySystemSound(1104) 
    UIView.animateWithDuration(0.2, animations: { 
     button.transform = CGAffineTransformScale(CGAffineTransformIdentity, 2.0, 2.0) 
     }, completion: {(_) -> Void in 
      button.transform = 
      CGAffineTransformScale(CGAffineTransformIdentity, 1, 1) 
    }) 
} 

感谢您的任何意见或建议...

+0

为什么每次按下按键时都处理系统声音?这似乎效率很低.. – lxt 2014-11-02 04:53:17

+0

我试过在didLoad处置,但我得到了相同的结果。 – jezuz 2014-11-02 04:56:41

+0

我基于以下代码:https://www.dropbox.com/s/gfewd0qekxr5u4i/CustomKeyboardFinal.zip?dl=0 – jezuz 2014-11-02 05:18:41

回答

0

你需要另一个线程来播放声音。用户需要允许完全访问才能工作。所以一定要确保你为这个在扩展

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),  
{ 
    AudioServicesPlaySystemSound(1104); 
});  

对于Objective-C中的info.plist中要求只是添加了^符号中的{

0

你需要移动AudioServicesDisposeSystemSoundID(1104)deinit方法前。