问题
I am making a customKeyboard which have delete button for delete a text and code for that is:
func addDelete() {
deleteButton = UIButton.buttonWithType(.System) as UIButton
deleteButton.setTitle(" Delete ", forState: .Normal)
deleteButton.sizeToFit()
deleteButton.setTranslatesAutoresizingMaskIntoConstraints(false)
deleteButton.addTarget(self, action: "didTapDelete", forControlEvents: .TouchUpInside)
deleteButton.layer.cornerRadius = 5
view.addSubview(deleteButton)
var rightSideConstraint = NSLayoutConstraint(item: deleteButton, attribute: .Right, relatedBy: .Equal, toItem: view, attribute: .Right, multiplier: 1.0, constant: -10.0)
var topConstraint = NSLayoutConstraint(item: deleteButton, attribute: .Top, relatedBy: .Equal, toItem: view, attribute: .Top, multiplier: 1.0, constant: +10.0)
view.addConstraints([rightSideConstraint, topConstraint])
}
func didTapDelete() {
var proxy = textDocumentProxy as UITextDocumentProxy
proxy.deleteBackward()
}
But this is slow because I have to click so many time to delete lots of characters from textField
.
Is there any Way to create a button which I can press continuously and it can delete a character so fast like default iPhone keyBoard backSpace button do?
回答1:
I Found the answer from HERE.
In this keyBoard the deleteButton have same functionality as I want.
Code for that button is:
private func addDeleteButton() {
deleteButton = KeyButton(frame: CGRectMake(keyWidth * 8.5 + spacing * 9.5, keyHeight * 2.0 + spacing * 3.0 + predictiveTextBoxHeight, keyWidth * 1.5, keyHeight))
deleteButton.setTitle("\u{0000232B}", forState: .Normal)
deleteButton.addTarget(self, action: "deleteButtonPressed:", forControlEvents: .TouchUpInside)
self.view.addSubview(deleteButton)
let deleteButtonLongPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: "handleLongPressForDeleteButtonWithGestureRecognizer:")
deleteButton.addGestureRecognizer(deleteButtonLongPressGestureRecognizer)
let deleteButtonSwipeLeftGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "handleSwipeLeftForDeleteButtonWithGestureRecognizer:")
deleteButtonSwipeLeftGestureRecognizer.direction = .Left
deleteButton.addGestureRecognizer(deleteButtonSwipeLeftGestureRecognizer)
}
func deleteButtonPressed(sender: KeyButton) {
switch proxy.documentContextBeforeInput {
case let s where s?.hasSuffix(" ") == true: // Cursor in front of tab, so delete tab.
for i in 0..<4 { // TODO: Update to use tab setting.
proxy.deleteBackward()
}
default:
proxy.deleteBackward()
}
updateSuggestions()
}
func handleLongPressForDeleteButtonWithGestureRecognizer(gestureRecognizer: UILongPressGestureRecognizer) {
switch gestureRecognizer.state {
case .Began:
if deleteButtonTimer == nil {
deleteButtonTimer = NSTimer(timeInterval: 0.1, target: self, selector: "handleDeleteButtonTimerTick:", userInfo: nil, repeats: true)
deleteButtonTimer!.tolerance = 0.01
NSRunLoop.mainRunLoop().addTimer(deleteButtonTimer!, forMode: NSDefaultRunLoopMode)
}
default:
deleteButtonTimer?.invalidate()
deleteButtonTimer = nil
updateSuggestions()
}
}
func handleSwipeLeftForDeleteButtonWithGestureRecognizer(gestureRecognizer: UISwipeGestureRecognizer) {
// TODO: Figure out an implementation that doesn't use bridgeToObjectiveC, in case of funny unicode characters.
if let documentContextBeforeInput = proxy.documentContextBeforeInput as NSString? {
if documentContextBeforeInput.length > 0 {
var charactersToDelete = 0
switch documentContextBeforeInput {
case let s where NSCharacterSet.letterCharacterSet().characterIsMember(s.characterAtIndex(s.length - 1)): // Cursor in front of letter, so delete up to first non-letter character.
let range = documentContextBeforeInput.rangeOfCharacterFromSet(NSCharacterSet.letterCharacterSet().invertedSet, options: .BackwardsSearch)
if range.location != NSNotFound {
charactersToDelete = documentContextBeforeInput.length - range.location - 1
} else {
charactersToDelete = documentContextBeforeInput.length
}
case let s where s.hasSuffix(" "): // Cursor in front of whitespace, so delete up to first non-whitespace character.
let range = documentContextBeforeInput.rangeOfCharacterFromSet(NSCharacterSet.whitespaceCharacterSet().invertedSet, options: .BackwardsSearch)
if range.location != NSNotFound {
charactersToDelete = documentContextBeforeInput.length - range.location - 1
} else {
charactersToDelete = documentContextBeforeInput.length
}
default: // Just delete last character.
charactersToDelete = 1
}
for i in 0..<charactersToDelete {
proxy.deleteBackward()
}
}
}
updateSuggestions()
}
来源:https://stackoverflow.com/questions/28024197/when-making-a-custom-ios-keyboard-extension-how-can-i-create-a-button-that-has