Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/103.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
定制iOS键盘&x2013;钥匙太慢了_Ios_Swift_Performance_Keyboard - Fatal编程技术网

定制iOS键盘&x2013;钥匙太慢了

定制iOS键盘&x2013;钥匙太慢了,ios,swift,performance,keyboard,Ios,Swift,Performance,Keyboard,我正在使用定制的iOS键盘,似乎无法加快按键的响应速度。我遇到的问题是,按键对触碰内部事件的响应有一个小而显著的延迟。标准iOS键盘(以及我下载的大多数自定义键盘)会立即响应。我所经历的短暂延迟导致了严重的可用性问题。这种延迟在模拟器中几乎不明显,但在设备(iPhone)上,当你快速打字时,它会导致键盘丢失 我尝试了多种技术来解决速度问题: 我使用约束构建了一次键盘,然后放弃了它,转而使用基于帧的布局(我是基于此做出选择的) 将“优化级别”的编译器设置更改为“最快/最小”(在调试和发布时) 使

我正在使用定制的iOS键盘,似乎无法加快按键的响应速度。我遇到的问题是,按键对触碰内部事件的响应有一个小而显著的延迟。标准iOS键盘(以及我下载的大多数自定义键盘)会立即响应。我所经历的短暂延迟导致了严重的可用性问题。这种延迟在模拟器中几乎不明显,但在设备(iPhone)上,当你快速打字时,它会导致键盘丢失

我尝试了多种技术来解决速度问题:

  • 我使用约束构建了一次键盘,然后放弃了它,转而使用基于帧的布局(我是基于此做出选择的)
  • 将“优化级别”的编译器设置更改为“最快/最小”(在调试和发布时)
  • 使这个类成为“final”(正如你在下面的代码中看到的那样)。我真的没想到这个能解决任何问题,但我已经绝望了
如何加速键盘按键?键盘的大部分代码如下(我只包括QWERTY键盘的功能,但数字键盘和符号键盘也有类似的功能)

要创建该项目,我只需创建一个新的iOS项目,为键盘添加一个扩展,并编写以下类:

    import UIKit

    final class KeyboardViewController: UIInputViewController {

var screenWidth : CGFloat = CGFloat()
var capsLockOn : Bool = false

override func viewDidLoad() {
    super.viewDidLoad()

    // Perform custom UI setup here
    view.backgroundColor = UIColor(red: 209 / 255, green: 213 / 255, blue: 219 / 255, alpha: 1)

    showQWERTYKeyboard()
}

func showQWERTYKeyboard() {

    //Setup some constants and variables that will be used for layout
    var buttonWidth : CGFloat = 26
    let buttonHeight : CGFloat = 40
    let standardButtonWidth : CGFloat = buttonWidth
    let horizontalSpaceBetweenButtons : CGFloat = 6.0
    let verticalSpaceBetweenButtons : CGFloat = 18

    var nextY : CGFloat = 0 //Tracks the current vertical position so we know where to place the next button
    var nextX : CGFloat = 1.5 //Tracks the current horizontal position so we know where to place the next button
    let standardNextX : CGFloat = nextX //Placeholder for the "nextX" value for the left-most key
    var buttonStrings : [String] = [""] //Holds the row of keys that get put on the screen

    //This outer loop iterates over each of the 4 rows. The inner loop below iterates over each letter in the array.
    var rowCounter : Int = Int()
    for rowCounter = 0; rowCounter<4; rowCounter++ {

        switch rowCounter {
        case 0:
            if capsLockOn {
                buttonStrings = ["Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P"]
            }
            else {
                buttonStrings = ["q", "w", "e", "r", "t", "y", "u", "i", "o", "p"]
            }
            nextY = 4
            nextX = standardNextX
        case 1:
            if capsLockOn {
                buttonStrings = ["A", "S", "D", "F", "G", "H", "J", "K", "L"]
            }
            else {
                buttonStrings = ["a", "s", "d", "f", "g", "h", "j", "k", "l"]
            }
            nextY = (CGFloat(rowCounter) * buttonHeight) + CGFloat((rowCounter))*verticalSpaceBetweenButtons
            nextX = (((buttonWidth + horizontalSpaceBetweenButtons) * 1) + (2*4))/2
        case 2:
            if capsLockOn {
                buttonStrings = ["^^", "Z", "X", "C", "V", "B", "N", "M", "<<"]
            }
            else {
                buttonStrings = ["^^", "z", "x", "c", "v", "b", "n", "m", "<<"]
            }
            nextY = (CGFloat(rowCounter) * buttonHeight) + CGFloat((rowCounter))*verticalSpaceBetweenButtons
            nextX = standardNextX
        case 3:
            buttonStrings = ["8", "kb", "sp", "rt"]
            nextY = (CGFloat(rowCounter) * buttonHeight) + CGFloat((rowCounter))*verticalSpaceBetweenButtons
            nextX = standardNextX
        default:
            break
        }

        //This inner loop iterates the current buttonStrings array and adds a target to the buttons, styles the buttons and adds the buttons to the view
        for buttonString in buttonStrings {
            //Create a new button
            let button : UIButton = UIButton(type: UIButtonType.Custom)

            styleAButton(button, buttonTitle: buttonString) //Customize the look and feel of the button

            switch buttonString {
            case "sp":
                button.addTarget(self, action: "touchUpInsideSpace:", forControlEvents: UIControlEvents.TouchUpInside)
                buttonWidth = (standardButtonWidth + horizontalSpaceBetweenButtons) * 5
            case "kb", "KB":
                button.addTarget(self, action: "advanceToNextInputMode", forControlEvents: UIControlEvents.TouchUpInside)
                buttonWidth = standardButtonWidth * 1.33
            case "rt":
                button.addTarget(self, action: "touchUpInsideReturn:", forControlEvents: UIControlEvents.TouchUpInside)
                buttonWidth = standardButtonWidth * 2.55
            case "^^":
                button.addTarget(self, action: "touchUpInsideCapsLock:", forControlEvents: UIControlEvents.TouchUpInside)
                buttonWidth = standardButtonWidth * 1.33
            case "<<":
                button.addTarget(self, action: "touchUpInsideBackspace:", forControlEvents: UIControlEvents.TouchUpInside)
                buttonWidth = standardButtonWidth * 1.33
                nextX = nextX + 7
            case "8":
                button.addTarget(self, action: "touchUpInsideNumbers:", forControlEvents: UIControlEvents.TouchUpInside)
                buttonWidth = standardButtonWidth * 1.33
            case "z", "Z": //Special case to simplify layout of the caps lock button
                button.addTarget(self, action: "touchUpInsideLetter:", forControlEvents: UIControlEvents.TouchUpInside)
                buttonWidth = standardButtonWidth
                nextX = nextX + 7
            default:
                button.addTarget(self, action: "touchUpInsideLetter:", forControlEvents: UIControlEvents.TouchUpInside)
                buttonWidth = standardButtonWidth
            }

            //Make the button
            button.frame = CGRectMake(nextX, nextY, buttonWidth, buttonHeight)

            //Determine horizontal placement for the next key
            nextX = nextX + horizontalSpaceBetweenButtons + buttonWidth

            //Add button to the view
            self.view.addSubview(button)
        }
    }
}
    func styleAButton(button : UIButton, buttonTitle : String) {



    //Configure button look and feel
    button.backgroundColor = UIColor.whiteColor()
    button.setTitle(buttonTitle, forState: UIControlState.Normal)
    button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
    button.titleLabel?.font = UIFont.systemFontOfSize(23.0)

    button.layer.cornerRadius = 4.0
    button.layer.masksToBounds = true
    button.layer.shadowColor = UIColor.lightGrayColor().CGColor
    button.layer.shadowOpacity = 0.8
    button.layer.shadowRadius = 6.0
    button.layer.shadowOffset = CGSizeMake(1.0, 1.0)
    button.contentEdgeInsets = UIEdgeInsetsMake(-3, -3, -3, -3)

}

//Shows the QWERTY keyboard
func touchUpInsideLetters(sender : UIButton) {
    for someView in view.subviews {
        someView.removeFromSuperview()
    }
    showQWERTYKeyboard()
}

//Shows the "numbers" keyboard
func touchUpInsideNumbers(sender : UIButton) {
    for someView in view.subviews {
        someView.removeFromSuperview()
    }
    showNumericKeyboard()
}

//Shows the keyboard with characters like [ ] { } etc.
func touchUpInsideMoreSymbols(sender : UIButton) {
    for someView in view.subviews {
        someView.removeFromSuperview()
    }
    showMoreSymbolsKeyboard()
}

//inserts the character of the key being pressed
func touchUpInsideLetter(sender : UIButton) {
    self.textDocumentProxy.insertText((sender.titleLabel?.text)!)

}

//inserts a space character
func touchUpInsideSpace(sender : UIButton) {
    let textToInsert : String = " "
    self.textDocumentProxy.insertText(textToInsert)

}

//inserts a new line character
func touchUpInsideReturn(sender : UIButton) {
    let textToInsert : String = "\n"
    self.textDocumentProxy.insertText(textToInsert)

}

//Toggles the global capsLockOn variable to be true or false (depending on current state).
func touchUpInsideCapsLock(sender : UIButton) {
    capsLockOn = !capsLockOn
    showQWERTYKeyboard()

}

//Inserts a backspace
func touchUpInsideBackspace(sender : UIButton) {
    (textDocumentProxy as UIKeyInput).deleteBackward()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated
}
    } //End of class
导入UIKit
最终类KeyboardViewController:UIInputViewController{
变量屏幕宽度:CGFloat=CGFloat()
var capsLockOn:Bool=false
重写func viewDidLoad(){
super.viewDidLoad()
//在此处执行自定义UI设置
view.backgroundColor=UIColor(红色:209/255,绿色:213/255,蓝色:219/255,alpha:1)
showQWERTYKeyboard()
}
func showQWERTYKeyboard(){
//设置一些用于布局的常量和变量
变量按钮宽度:CGFloat=26
let buttonHeight:CGFloat=40
让standardButtonWidth:CGFloat=buttonWidth
让按钮之间的水平间距为:CGFloat=6.0
让按钮之间的垂直间距为:CGFloat=18
var nextY:CGFloat=0//跟踪当前垂直位置,以便我们知道下一步按钮的位置
var nextX:CGFloat=1.5//跟踪当前水平位置,以便我们知道下一步按钮的位置
让standardNextX:CGFloat=nextX//占位符作为最左边键的“nextX”值
var buttonStrings:[String]=[“”]//保存在屏幕上的一行键
//这个外循环遍历4行中的每一行。下面的内循环遍历数组中的每个字母。
变量行计数器:Int=Int()

对于ROUTROCK=0;RealOffice < P>也许,如果代码> ToucUpIn < /COD>太长,你可能会考虑<代码>触地事件吗?< /P> < P>我只是想出来了!奇怪的是,除非你配置了SythOpTrand属性,否则绘制一个阴影是非常昂贵的。方法,所以我添加了如下内容:

    button.layer.shadowPath = UIBezierPath(rect: button.bounds).CGPath
关于更多细节,这里有一个由某人撰写的文章,讨论了一些幕后发生的事情


我猜测这可能与按钮的样式有关,从而将问题隔离开来。这在当时对我来说没有多大意义,但现在我明白了,因为按钮在按键时设置了动画,所以它在阴影上绘制了一些图形,因为这是一个昂贵的操作,它会减慢角色的插入速度(它可能阻塞了主线程).

标准iOS键盘只有在手指从键盘上松开后才会插入按键。触地键调用该函数,在手指放在键盘上后立即插入按键。我希望它能像标准键盘一样工作,所以我想我希望它能在触地键内部工作。这很公平。只是一个建议。空格和返回键是否也会延迟与字母一样多,还是只是字母滞后?我想知道
发送者。titleLabel?.text
呼叫
触碰Insideletter
是不是让你慢下来了?可能是通过
标签找到按钮,然后使用
开关
?Tim,这是个好主意,但不幸的是,空格键和返回键都是演示阴影优化是一个众所周知的问题。苹果公司在文档中也指出了这一点。