斯威夫特的iOS条纹 - 如何关闭键盘时,过期或CVV领域是主动和卡号字段为空

问题描述 投票:0回答:1

我有接受卡号码,有效期,和CVV 1个条纹文本字段。我也有观点敲击手势识别器来消除键盘。

如果卡是空号或填写不正确(如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)
}
ios swift uitextfield stripe-payments uikeyboard
1个回答
0
投票

我不知道条纹是如何工作的,但试试这个:

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

© www.soinside.com 2019 - 2024. All rights reserved.