我想在UITextField上双击编写自定义代码,并阻止键盘的默认文本编辑和弹出。我已经尝试了以下内容,到目前为止我没有任何工作。请帮我解决这个问题。
let gestureArray = NamTxtBoxVal.gestureRecognizers
var tapGesture = UITapGestureRecognizer()
for idxVar in gestureArray!
{
if let tapVar = idxVar as? UITapGestureRecognizer
{
if tapVar.numberOfTapsRequired == 2
{
tapGesture = tapVar
NamTxtBoxVal.removeGestureRecognizer(tapGesture)
}
}
}
let doubleTap = UITapGestureRecognizer(target: self, action: #selector(namFnc(_:)))
doubleTap.numberOfTapsRequired = 2
doubleTap.delegate = self
tapGesture.requireGestureRecognizerToFail(doubleTap)
NamTxtBoxVal.addGestureRecognizer(doubleTap)
我也尝试过:
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool
{
return false
}
答案 0 :(得分:1)
我知道你这样做的唯一方法是
这将允许您在TextField区域注册双击,而不是弹出任何键盘或进入编辑模式。
不确定双击后你打算用textField做什么,但是你应该能够以编程方式处理大部分内容。
此代码为:
class ViewController: UIViewController {
@IBOutlet weak var myViewBehindMyTextField: UIView!
@IBOutlet weak var myTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.myTextFieldTapped(_:)))
tapGesture.numberOfTapsRequired = 2
myViewBehindMyTextField.addGestureRecognizer(tapGesture)
}
func myTextFieldTapped(sender: UITapGestureRecognizer) {
print("Double tapped on textField")
}
}