Swift iOS Stripe-在“到期”或“ CVV”字段为活动且卡号字段为空的情况下如何关闭键盘

时间:2019-02-03 05:48:19

标签: ios swift uitextfield stripe-payments uikeyboard

我有1个带区的文本字段,可以接受卡号,到期日期和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)
}

1 个答案:

答案 0 :(得分:0)

我不知道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