jsdw_ios/QuickLocation/Section/Home/SignIn/InputEmailPopupView.swift

180 lines
5.3 KiB
Swift

//
// InputEmailPopupView.swift
// QuickLocation
//
// Created by on 2026/6/30.
//
import UIKit
import RxSwift
import RxCocoa
class InputEmailPopupView: UIView {
private static let shared = InputEmailPopupView(frame: CGRect(origin: .zero, size: kScreenSize))
var disposeBag = DisposeBag()
private var completion: ((String) -> Void)?
static func show(completion: @escaping ((String) -> Void)) {
guard let superView = kKeyWindow else {
return
}
if InputEmailPopupView.shared.superview != nil {
InputEmailPopupView.shared.removeFromSuperview()
InputEmailPopupView.shared.bgView.frame = .zero
}
InputEmailPopupView.shared.bgView.alpha = 1
InputEmailPopupView.shared.bgView.frame = CGRect(x: 0, y: 0, width: kScreenWidth, height: kScreenHeight)
superView.addSubview(InputEmailPopupView.shared)
superView.bringSubviewToFront(InputEmailPopupView.shared)
UIView.animate(withDuration: 0.25) {
InputEmailPopupView.shared.bgView.alpha = 1
}
InputEmailPopupView.shared.completion = { text in
completion(text)
InputEmailPopupView.dismiss()
}
}
///
static func dismiss() {
guard InputEmailPopupView.shared.superview != nil else { return }
UIView.animate(withDuration: 0.25, delay: 0, options: [.curveEaseIn]) {
InputEmailPopupView.shared.bgView.alpha = 0
} completion: { _ in
InputEmailPopupView.shared.removeFromSuperview()
}
}
private func setupRx() {
emailTF.rx.controlEvent(.editingDidEndOnExit)
.subscribe(onNext: { [weak self] in
guard let self = self else { return }
self.emailTF.resignFirstResponder()
})
.disposed(by: disposeBag)
confirmBtn.rx.tap.subscribe(onNext: { _ in
guard let text = self.emailTF.text, String.checkEmailAddressIsValid(text) else {
DLToast.show(text: "请输入正确的邮箱")
return
}
self.completion?(text)
}).disposed(by: disposeBag)
cancelBtn.rx.tap.subscribe(onNext: { _ in
InputEmailPopupView.dismiss()
}).disposed(by: disposeBag)
}
private lazy var bgView: UIView = {
let view = UIView()
view.backgroundColor = .black.withAlphaComponent(0.5)
return view
}()
lazy var infoView: UIView = {
let view = UIView()
view.backgroundColor = .white
view.cornerRadius = 15
return view
}()
lazy var titleLab: UILabel = {
let label = UILabel()
label.text = "添加邮箱"
label.textColor = UIColor(hexStr: "#333333")
label.font = .systemFont(ofSize: 20, weight: .medium)
return label
}()
lazy var textFieldView: UIView = {
let view = UIView()
view.cornerRadius = 10
view.backgroundColor = UIColor(hexStr: "#F5FBFF")
return view
}()
lazy var emailTF: UITextField = {
let textField = UITextField()
textField.font = .systemFont(ofSize: 16, weight: .medium)
textField.placeholder = "请添加邮箱地址"
textField.returnKeyType = .done
return textField
}()
lazy var confirmBtn: UIButton = {
let btn = UIButton(type: .custom)
btn.setTitle("确定", for: .normal)
btn.setTitleColor(.white, for: .normal)
btn.titleLabel?.font = .systemFont(ofSize: 16, weight: .medium)
btn.setBackgroundImage(UIImage(named: "Common/button_bg_2"), for: .normal)
btn.cornerRadius = 25
return btn
}()
lazy var cancelBtn: UIButton = {
let btn = UIButton(type: .custom)
btn.setTitle("取消", for: .normal)
btn.setTitleColor(UIColor(hexStr: "#16B3FF"), for: .normal)
btn.titleLabel?.font = .systemFont(ofSize: 16, weight: .medium)
btn.backgroundColor = .clear
return btn
}()
// MARK: - Init
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = .clear
addSubview(bgView)
bgView.addSubview(infoView)
infoView.addSubview(titleLab)
infoView.addSubview(textFieldView)
textFieldView.addSubview(emailTF)
infoView.addSubview(confirmBtn)
infoView.addSubview(cancelBtn)
infoView.layoutChain
.edgesHorzontal(30)
.heightToWidth(296/315)
.centerY()
titleLab.layoutChain
.top(24)
.centerX()
textFieldView.layoutChain
.topToBottomOfView(titleLab, offset: 28)
.edgesHorzontal(20)
.height(62)
emailTF.layoutChain
.edgesHorzontal(15)
.edgesVertical()
confirmBtn.layoutChain
.topToBottomOfView(textFieldView, offset: 46)
.edgesHorzontal(20)
.height(50)
cancelBtn.layoutChain
.topToBottomOfView(confirmBtn)
.edgesHorzontal(20)
.height(50)
setupRx()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}