我正在使用自定义iOS键盘,似乎无法加快对键的响应速度。我遇到的问题是密钥响应touchUpInside事件的延迟很小但很明显。标准的iOS键盘(以及我下载的大多数自定义键盘)立即响应。我遇到的短暂延迟会导致严重的可用性问题。延迟在模拟器中几乎不会引起注意,但在设备(iPhone)上,当您快速键入时,它会导致错过键。
我尝试了各种技术来解决速度问题:
如何加快键盘按键的速度?键盘的大部分代码都在下面(我只包括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开发的新手,我希望我能错过一些明显的东西。
答案 0 :(得分:3)
我刚想通了!令人惊讶的是,除非配置shadowPath属性,否则绘制阴影非常昂贵。我没有在我的&#34; styleAButton&#34;中做到这一点。方法,所以我这样添加:
button.layer.shadowPath = UIBezierPath(rect: button.bounds).CGPath
如需了解更多详细信息,可由某人post讨论幕后发生的事情。
我能够通过猜测可能与按钮样式有关来隔离问题。那对我来说当然没有多大意义,但是现在我明白了,因为按钮在按键上是动画的,所以它在那个阴影上做了一些绘画,因为它是这是一个昂贵的操作,它减慢了角色的插入速度(它可能阻塞了主线程)。
答案 1 :(得分:0)
如果On Class where Date format is required (Implementation class)
-(void)DateFormatter:stringDateToBeFormatted
{
NSDate *dateFormatter=[NSDate convertStringToDate:stringDateToBeFormatted];
NSString *requiredDateFormat= [NSDate dateStringWithMediumFormat: dateFromatter];
self.stringDateFormatLable.Text=[NSString stringWithFormat:@"date required %@",requiredDateFormat]
}
On NSDate Class implementation class
+(NSString *)dateStringWithMediumFormat:(NSDate *)date
{
NSDateFormatter *dateFormatter =[[NSDateFormatter alloc]init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
or [dateFormatter setDateFormat:@"MMM-dd-yyyy"];
return [dateFormatter stringFromDate:date];
}
+(NSDate *)convertStringToDate:(NSString *)dateString
{
NSDateFormatter *dateFormatter= [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'hh':'mm':'ss"];
return [dateFormatter dateFromString:dateString];
}
花费的时间过长,您可能会考虑使用touchupInside
事件吗?