我正在做一个定制的键盘。单击键盘上的delete键即可正常工作。我想实现对delete键的长按,这样当用户按住delete键时,键盘就会像标准ios键盘一样连续删除。我在Stackoverflow上提到了一些解决方案,比如https://stackoverflow.com/a/26234876/6077720、https://stackoverflow.com/a/25633313/6077720、https://stackoverflow.com/a/30711421/6077720
但这些对我来说都不管用。我还尝试了下面的代码:
override func viewDidLoad() {
super.viewDidLoad()
textDocument = self.textDocumentProxy
var longPress = UILongPressGestureRecognizer(target: self, action: #selector(self.longPress))
self.deleteKeyPressed.addGestureRecognizer(longPress)
}
func longPress(gesture: UILongPressGestureRecognizer) {
if gesture.state == .Ended {
print("Long Press")
self.textDocumentProxy.deleteBackward()
}
}但是在写完这段代码后,我的键盘并不只出现在屏幕上。有谁能帮帮我吗?
发布于 2016-08-31 20:21:09
尝试下面的代码
var timer: NSTimer?
override func viewDidLoad() {
super.viewDidLoad()
textDocument = self.textDocumentProxy
var longPressRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(KeyboardViewController.longPressHandler(_:)))
eraseButton.addGestureRecognizer(longPressRecognizer)
}
func longPressHandler(gesture: UILongPressGestureRecognizer) {
if gesture.state == .Began {
timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: #selector(KeyboardViewController.handleTimer(_:)), userInfo: nil, repeats: true)
} else if gesture.state == .Ended || gesture.state == .Cancelled {
timer?.invalidate()
timer = nil
}
}
func handleTimer(timer: NSTimer) {
self.deleteText()
}发布于 2017-02-16 01:24:31
override func viewDidLoad() {
super.viewDidLoad()
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(KBViewController.handleLongPress(_:)))
longPress.minimumPressDuration = 0.5
longPress.numberOfTouchesRequired = 1
longPress.allowableMovement = 0.5
row3B11.addGestureRecognizer(longPress)}
func handleLongPress(_ gestureRecognizer: UIGestureRecognizer) {
textDocumentProxy.deleteBackward()
}发布于 2018-07-27 14:26:47
你的代码没问题
请从您的代码中删除结束手势条件,它将正常工作
@objc func btnDeleteLongPress(gesture : UILongPressGestureRecognizer)
{
self.textDocumentProxy.deleteBackward()
}https://stackoverflow.com/questions/39247922
复制相似问题