gpt4 book ai didi

ios - 自定义 iOS 键盘 – 按键太慢

转载 作者:行者123 更新时间:2023-11-28 11:12:08 25 4
gpt4 key购买 nike

我正在使用自定义 iOS 键盘,但似乎无法加快按键响应速度。我遇到的问题是按键对 touchUpInside 事件的响应有一个小但显着的延迟。标准 iOS 键盘(以及我下载的大多数自定义键盘)会立即响应。我遇到的短暂延迟会导致严重的可用性问题。延迟在模拟器中几乎察觉不到,但在设备 (iPhone) 上,它会导致您在快速打字时遗漏按键。

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

  • 我已经使用约束构建了一次键盘,然后放弃它以支持这种基于框架的布局(我根据这个 writeup 做出了选择
  • 将“优化级别”的编译器设置更改为“最快/最小”(在调试和发布时)
  • 使这个类成为“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

感谢您提供的任何帮助。我是 iOS 开发的新手,我希望我只是遗漏了一些明显的东西。

最佳答案

我刚刚知道了!令人惊讶的是,除非您配置 shadowPath 属性,否则绘制阴影的开销非常大。我没有在我的“styleAButton”方法中这样做,所以我这样添加它:

    button.layer.shadowPath = UIBezierPath(rect: button.bounds).CGPath

有关更多详细信息,请参阅 post由某人讨论幕后发生的事情。

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

关于ios - 自定义 iOS 键盘 – 按键太慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34324496/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com