首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自定义iOS键盘-键太慢了

自定义iOS键盘-键太慢了
EN

Stack Overflow用户
提问于 2015-12-17 00:09:42
回答 2查看 1.1K关注 0票数 2

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

我尝试过各种技术来解决速度问题:

  • 我使用约束构建了键盘一次,然后放弃它以支持基于帧的布局(我基于这个记记做出了选择)。
  • 将“优化级别”的编译器设置更改为“最快/最小”(在调试和发布时)
  • 使这个类成为“最终”(如您在下面的代码中所看到的)。我没料到这能解决任何问题,但我已经绝望了。

我怎样才能加快键盘键的速度?键盘的大部分代码如下(我只包含了QWERTY键盘的函数,但数字键盘和符号键盘存在类似的函数)

为了创建该项目,我简单地创建了一个新的iOS项目,为键盘添加了一个扩展,并编写了以下类:

代码语言:javascript
复制
    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

谢谢你能提供的任何帮助。我是iOS开发的新手,我希望我只是缺少一些显而易见的东西。

EN

Stack Overflow用户

发布于 2015-12-17 00:16:13

也许,如果touchupInside花费的时间太长,您可能会考虑touchDown事件吗?

票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34324496

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档