自定义键盘扩展的慢速按钮

时间:2018-04-01 09:09:09

标签: ios swift ios-keyboard-extension

我们制作了自定义键盘并上传到App Store,设计和键盘工作正常。然而,由于按钮的缓慢,我们得到了很多一星评价。问题是。有没有办法加快UIButtons的速度?

一旦用户将手指从按钮上移开,UIControl的touchUpInside就会起作用,在某些情况下,人们会写得如此之快,而touchUpInside不是"快速&#34 ;做的方式

TouchDown将在TouchUpInside之前触发,因为触摸是手指触摸手机的动作。

请就此问题提出建议,您通常更喜欢生产键盘的方式和方式

这是我目前处理水龙头的方法:这太慢了!

func buttonAction(_ sender:UIButton!) {
        print("Button tapped")
       self.textDocumentProxy.insert("B")
}

2 个答案:

答案 0 :(得分:1)

TouchDown TouchUpInside 的速度并不是真正的问题。当用户使用两个拇指键入并且按键顺序解释错误时,就会出现问题。

Apple的默认键盘在触摸时注册了一个键。但是,在iPhone上,如果在第一个键被按下时按下第二个键,则第一个键被注册,然后不等待其触摸。这使得输入保持按下触摸顺序(对于两个拇指键入),但仍然反映了触摸行为。

要实现这一点,您需要同时观察 TouchDown TouchUpInside 事件。

这是你可以做到的一种方式。创建一个名为pendingButton的属性来跟踪按下的最后一个按钮,并在该按钮松开或按下另一个按钮时处理该按钮。

您需要将buttonDown连接到 TouchDown 事件,并将buttonUp连接到 TouchUpInside 事件。

// last button pressed down
var pendingButton: String?

// connect button TouchDown events here    
@IBAction func buttonDown(_ sender: UIButton) {
    // If we already have a pending button, process it before storing
    // the next one
    if let pending = self.pendingButton {
        self.textDocumentProxy.insert(pending)
    }
    self.pendingButton = sender.currentTitle
}

// connect button TouchUpInside events here   
@IBAction func buttonUp(_ sender: UIButton) {
    // If the button being let up is the latest pending button,
    // then process it, otherwise ignore it
    if sender.currentTitle == self.pendingButton {
        self.textDocumentProxy.insert(self.currentTitle!)
        self.pendingButton = nil
    }
}

注意:您可能还需要仔细考虑其他事件,例如 TouchUpOutside 。这可能应该与buttonUp连接,具体取决于键盘的所需行为。

如果相反,在按钮外拖动会取消按钮,然后您应该实现一个功能来观察 TouchDragExit ,然后取消待处理按钮(如果这是待处理按钮)。

// connect button TouchDragExit events here
@IBAction func dragExit(_ sender: UIButton) {
    if sender.currentTitle == self.pendingButton {
        self.pendingButton = nil
    }
}

答案 1 :(得分:1)

加快使用调度队列的最简单方法

 DispatchQueue.main.async { 
   self.textDocumentProxy.insert(self.currentTitle!)}

我做到了,并且获得了像原始键盘一样的速度