快速长按自定义键盘的删除键

2024-01-31

我正在制作一个定制键盘。键盘上的删除键单击即可正常工作。但它不适用于长按。我想实现长按删除键,以便当用户按住删除按钮时,键盘会像标准ios键盘一样连续删除。我在 Stackoverflow 上提到了几个解决方案,例如:https://stackoverflow.com/a/26234876/6077720 https://stackoverflow.com/a/26234876/6077720, https://stackoverflow.com/a/25633313/6077720 https://stackoverflow.com/a/25633313/6077720, https://stackoverflow.com/a/30711421/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()

    }
}

但写完这段代码后,我的键盘不仅不出现。谁能帮帮我吗?


试试下面的代码

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()
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

快速长按自定义键盘的删除键 的相关文章

随机推荐