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

送分小仙女□ 提交于 2020-01-02 04:05:12

问题


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!


回答1:


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 */
    }
}


来源:https://stackoverflow.com/questions/30710757/how-can-we-use-long-press-to-delete-whole-words-for-custom-keyboards-in-ios-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!