我有一个条形文本字段,它接受卡号、到期日期和cvv。我在视图上还有一个敲击手势识别器,可以关闭键盘。
如果卡号为空或填写不正确(例如,4位数字而不是16位),我按下到期日期或cvv字段,然后按下文本字段外以关闭键盘,而如果它立即关闭,则跳到卡号字段,则我必须再次按下文本字段以外以关闭键盘。从本质上讲,我必须在键盘外按两次才能消除它,这看起来像是一个bug。
然而,如果卡号填写正确,然后我经过同样的过程,我只需要按下文本字段外的一次就可以关闭键盘。
似乎STPPaymentCardTextField
上有一个默认值,上面写着"如果卡号无效,用户试图从到期日或cvv字段中取消键盘,那么不要取消键盘,先跳到卡号,然后只从那里取消">
即使卡号填写不正确,我如何立即将键盘从有效期或cvv字段中删除?
import Stripe
let paymentTextField: STPPaymentCardTextField = {
let stp = STPPaymentCardTextField()
stp.translatesAutoresizingMaskIntoConstraints = false
return stp
}()
override func viewDidLoad() {
super.viewDidLoad()
paymentTextField.delegate = self
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
view.addGestureRecognizer(tapGesture)
}
@objc fileprivate func dismissKeyboard() {
view.endEditing(true)
}
我不知道Stripe是如何工作的,但试试这个:
override func viewDidLoad() {
super.viewDidLoad()
paymentTextField.delegate = self
setGestureRecognizerDelegate()
}
private func setGestureRecognizerDelegate() {
let tapOnEmptyPlaceGestureRecognizer = UITapGestureRecognizer(
target: self,
action: #selector(tapOnEmptyPlaceGestureCaptured(gesture:)))
tapOnEmptyPlaceGestureRecognizer.delegate = self
view.addGestureRecognizer(tapOnEmptyPlaceGestureRecognizer)
}
@objc private func tapOnEmptyPlaceGestureCaptured(gesture: UITapGestureRecognizer) {
let touchPoint = gesture.location(in: view)
// Make next code for all your textFields
if !paymentTextField.point(inside: touchPoint, with: nil) {
paymentTextField.resignFirstResponder()
}
}
另外,别忘了让你的UIViewController成为UIGestureRecognizerDelegate