| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- //
- // 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 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()
- }
-
- required init?(coder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
- }
- extension LNCaptchaInputView: UITextFieldDelegate, LNTextFieldDelegate {
- 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()
- }
-
- func textFieldDidChangeSelection(_ textField: UITextField) {
- guard textField.text != nil else { return }
-
- let endPosition = textField.endOfDocument
-
- if textField.selectedTextRange?.start != endPosition {
- textField.selectedTextRange = textField.textRange(from: endPosition, to: endPosition)
- }
- }
- }
- 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..<captchaCount {
- let container = UIView()
- container.layer.cornerRadius = 10
- container.backgroundColor = .fill
- container.snp.makeConstraints { make in
- make.width.equalTo(64)
- }
-
- let input = LNTextField()
- input.delegate = self
- input.exDelegate = self
- input.font = .systemFont(ofSize: 32, weight: .semibold)
- input.textColor = .text_5
- input.keyboardType = .numberPad
- input.textAlignment = .center
- input.addAction(UIAction(handler: { [weak self, weak input] _ in
- guard let self, let input else { return }
-
- guard let text = input.text, !text.isEmpty else { return }
- input.text = "\(Int(text.prefix(1)) ?? 0)"
- guard let index = inputViews.firstIndex(of: input) else { return }
- if index < inputViews.count - 1 {
- inputViews[index + 1].text = nil
- inputViews[index + 1].becomeFirstResponder()
- } else {
- input.resignFirstResponder()
- }
- delegate?.onCaptchaInputChange(view: self)
- }), for: .editingChanged)
- container.addSubview(input)
- input.snp.makeConstraints { make in
- make.horizontalEdges.equalToSuperview()
- make.centerY.equalToSuperview()
- }
-
- stackView.addArrangedSubview(container)
- inputViews.append(input)
- }
-
- inputViews.first?.becomeFirstResponder()
- }
- }
- private protocol LNTextFieldDelegate: NSObject {
- func onDeleteBackward(_ textField: UITextField, oldText: String?)
- }
- private class LNTextField: UITextField {
- weak var exDelegate: LNTextFieldDelegate?
-
- override func deleteBackward() {
- let oldText = text
-
- super.deleteBackward()
-
- exDelegate?.onDeleteBackward(self, oldText: oldText)
- }
- }
|