You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现自定义UIInputView的长按连续删除功能?

自定义UIInputView长按删除键实现问题

为UITextField使用自定义UIInputView时,希望实现和原生键盘一致的删除键效果:点击删除单个字符,长按持续删除,但长按功能未达到预期。

尝试的实现方案

  • 为删除按钮添加UILongPressGestureRecognizer手势识别器
  • 实现对应的动作方法:
@IBAction func longPressDelete(_ sender: UILongPressGestureRecognizer) {    
            Timer.scheduledTimer(withTimeInterval: 0.4, repeats: true) { timer in
                    switch sender.state {
                        case .began:
                            break
                        case .ended, .cancelled, .failed:
                            timer.invalidate()
                        default:
                            timer.invalidate()
                    }
                    self.deleteKeyPress?() //callback for delete one character called once.
                }
            }

遇到的问题

  • 效果与iOS原生键盘差距明显
  • 出现错误日志:
[KeyboardTaskQueue] -[UIKeyboardTaskQueue lockWhenReadyForMainThread] timeout waiting for task on queue
  • 需要松开手指重新按压,才能再次触发删除操作

解决方法

将UITextField直接传入自定义UIView,调用UIKeyInput协议中的deleteBackward()方法,解决了该问题。


内容的提问来源于stack exchange,提问作者William Hu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 07:42:02