//
|
// IDCardView.swift
|
// WanPai
|
//
|
// Created by 杨锴 on 2023/6/12.
|
//
|
|
import UIKit
|
|
class IDCardView: UIView {
|
|
var box:Box<String>!
|
|
private let VIEW_HEIGHT:Double = 200
|
private let SafeInserts = UIApplication.shared.delegate?.window?!.screen.focusedView?.safeAreaInsets ?? .zero
|
private var screenW = UIScreen.main.bounds.size.width
|
private var avgCellW:Double!
|
private var avgCellH:Double!
|
private var cellItems = ["1","2","3","4","5","6","7","8","9","X","0","Del"]
|
private var content:String = ""
|
private var btn_X:UIButton!
|
private var btn_Del:UIButton!
|
|
override init(frame: CGRect) {
|
super.init(frame: frame)
|
|
setUI()
|
box = Box()
|
}
|
|
private func setUI(){
|
avgCellW = screenW / 3.0
|
avgCellH = VIEW_HEIGHT / 4.0
|
frame = CGRect(origin: .zero, size: CGSize(width: screenW, height: VIEW_HEIGHT + SafeInserts.bottom))
|
backgroundColor = .white
|
|
for (index,item) in cellItems.enumerated() {
|
let i = Double(index).truncatingRemainder(dividingBy: 3.0)
|
let j = floor(Double(index) / 3.0)
|
let btn = createButton(text: item, indexTag: index)
|
btn.frame = CGRect(x: i * avgCellW, y: j * avgCellH, width: avgCellW, height: avgCellH)
|
btn.layer.borderColor = UIColor.gray.withAlphaComponent(0.2).cgColor
|
btn.layer.borderWidth = 0.5
|
btn.setTitleColor(.gray.withAlphaComponent(0.2), for: .disabled)
|
addSubview(btn)
|
}
|
}
|
|
private func createButton(text:String,indexTag:Int)->UIButton{
|
let btn = UIButton(type: .roundedRect)
|
if text == "Del"{
|
if #available(iOS 13.0, *) {
|
btn.setImage(UIImage(systemName: "delete.left")?.withTintColor(.black), for: .normal)
|
} else {
|
btn.setTitle("Del", for: .normal)
|
}
|
}else{
|
btn.setTitle(text, for: .normal)
|
}
|
btn.tag = 10 + indexTag
|
btn.titleLabel?.font = UIFont.systemFont(ofSize: 20, weight: .medium)
|
btn.setTitleColor(.black, for: .normal)
|
btn.addTarget(self, action: #selector(tapAction(_:)), for: .touchUpInside)
|
btn.setTitleColor(.gray, for: .highlighted)
|
|
if text == "X"{
|
btn_X = btn;btn_X.isEnabled = false
|
}
|
|
if text == "Del"{
|
btn_Del = btn
|
let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(longGesture))
|
btn_Del.addGestureRecognizer(longGesture)
|
}
|
|
return btn
|
}
|
|
@objc private func tapAction(_ btn:UIButton){
|
guard let text = btn.titleLabel?.text else {
|
if !content.isEmpty{content.removeLast()}
|
box.value = content
|
btn_X.isEnabled = content.count == 17
|
return
|
}
|
|
if content.count <= 17{
|
content.append(text)
|
}
|
btn_X.isEnabled = content.count == 17
|
box.value = content
|
}
|
|
@objc private func longGesture(_ gesture:UILongPressGestureRecognizer){
|
if content.count > 0{
|
content.removeLast()
|
box.value = content
|
}
|
|
}
|
|
required init?(coder: NSCoder) {
|
fatalError("init(coder:) has not been implemented")
|
}
|
}
|
|
final class Box<T> {
|
// 声明一个别名
|
typealias Listener = (T) -> Void
|
var listener: Listener?
|
|
var value: T? {
|
didSet {
|
guard let v = value else { return }
|
listener?(v)
|
}
|
}
|
|
init(_ value: T? = nil){
|
self.value = value
|
}
|
|
func bind(listener: Listener?) {
|
self.listener = listener
|
guard let v = value else { return }
|
listener?(v)
|
}
|
}
|