在 swift 中长按自定义键盘的删除键

Long press delete key of a custom keyboard in swift

我正在制作自定义键盘。键盘上的删除键适用于单击。但长时间不工作 press.I 想要实现长按删除键,以便当用户按住删除按钮时,键盘会像标准 ios 键盘一样连续删除。我在 Whosebug 上提到了几个解决方案,比如 - , ,

但是 none 它对我有用。我也试过这段代码:

  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()
}
 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()
}

你的代码没问题

请从您的代码中删除结束手势条件,它将正常工作

@objc func btnDeleteLongPress(gesture : UILongPressGestureRecognizer)
{
    self.textDocumentProxy.deleteBackward()
}