// // LNCaptchaInputView.swift // Gami // // Created by OneeChan on 2026/1/16. // import Foundation import UIKit import SnapKit protocol LNCaptchaInputViewDelegate: NSObject { func onCaptchaInputChange(view: LNCaptchaInputView) } class LNCaptchaInputView: UIView { private let captchaCount: Int = 4 private let stackView = UIStackView() private(set) var inputViews: [UITextField] = [] weak var delegate: LNCaptchaInputViewDelegate? var hasDone: Bool { inputViews.first { $0.text?.isEmpty != false } == nil } var curInput: String { inputViews.map { $0.text ?? "" }.joined() } override init(frame: CGRect) { super.init(frame: frame) setupViews() buildCaptchaInput() } func startInput() { inputViews.first?.becomeFirstResponder() } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } } extension LNCaptchaInputView: UITextFieldDelegate, LNTextFieldDelegate { func textFieldDidBeginEditing(_ textField: UITextField) { textField.text = nil } func onDeleteBackward(_ textField: UITextField, oldText: String?) { if let oldText, !oldText.isEmpty { return } guard let index = inputViews.firstIndex(of: textField) else { return } if index == 0 { return } inputViews[index - 1].text = nil inputViews[index - 1].becomeFirstResponder() } } extension LNCaptchaInputView { private func setupViews() { stackView.axis = .horizontal stackView.distribution = .equalCentering addSubview(stackView) stackView.snp.makeConstraints { make in make.edges.equalToSuperview() } } private func buildCaptchaInput() { let allViews = stackView.arrangedSubviews allViews.forEach { stackView.removeArrangedSubview($0) $0.removeFromSuperview() } inputViews.removeAll() for _ in 0..