Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can we use long press to delete whole words, for custom keyboards in iOS 8?

As we know, the original keyboard in iOS can delete whole words by holding down the delete button (⌫) for an extended period of time.
So how can we use the same functionality for custom keyboards in Swift, iOS 8?

Note:
I'm currently using proxy.deleteBackward() for deleting letters, and using:

var gesture = UILongPressGestureRecognizer(target: self, action: "longPressed:")
gesture.minimumPressDuration = 1.0
button.addGestureRecognizer(gesture)

when the button is pressed for a greater amount of time.

Thanks!

like image 595
He Yifei 何一非 Avatar asked Nov 09 '22 11:11

He Yifei 何一非


1 Answers

I'm not sure how you would be able to do it through gesture recognizer.

The original keyboard behaviour is,

  • When the button is pressed and kept pressed for initial X-time interval, it keeps deleting backward.
  • When the button is kept pressed after the initial X-time interval, it starts deleting words instead of just characters.

After the button is pressed the first time, you should probably keep calling your delete function and keep noticing if 'X-time-interval' has elapsed. Pseudocode would be

var startTime: NSDate = NSDate()
var timer: NSTimer?
func deleteButtonPressed(deleteButton: UIButton) {
    startTime = NSDate()
    timer = NSTimer.scheduledTimerWithTimeInterval(0.4, target: self, selector: Selector("delete"), userInfo: nil, repeats: true)
}

func delete() {
    if !deleteButton.highlighted {
        timer.invalidate()
        timer = nil
        return
    }

    if ((currentNSDate - startTime ) < "X-time-Interval") {
        // delete backward
    } else {
        /* figure out last space character in text and create NSRange
        then
        mytextView.text deleteCharactersInRange:theRange
        set new text */
    }
}
like image 200
Shamas S Avatar answered Nov 14 '22 22:11

Shamas S