jsdw_ios/QuickLocation/Section/Group/MemberInfo/GenderSelectPopVC.swift

179 lines
6.0 KiB
Swift

//
// GenderSelectPopVC.swift
// QuickLocation
//
import UIKit
final class GenderSelectPopVC: DLCustomPopVC {
var onConfirm: ((Int) -> Void)?
private var selectedSex: Int
private let options: [(sex: Int, title: String, on: String, off: String)] = [
(1, "", "MemberInfo/gender_male_on", "MemberInfo/gender_male_off"),
(2, "", "MemberInfo/gender_female_on", "MemberInfo/gender_female_off"),
(0, "保密", "MemberInfo/gender_secret_on", "MemberInfo/gender_secret_off")
]
private var optionButtons: [UIButton] = []
init(currentSex: Int, onConfirm: ((Int) -> Void)? = nil) {
self.selectedSex = currentSex
self.onConfirm = onConfirm
super.init(nibName: nil, bundle: nil)
popStyle = .bottom
bottomCornerRadius = 40
dimmingClick = true
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
setupUI()
refreshSelection()
}
private func setupUI() {
let headerBgImage = UIImageView()
headerBgImage.image = UIImage(named: "Common/popup_header_bg")
headerBgImage.contentMode = .scaleToFill
contentView.addSubview(headerBgImage)
headerBgImage.isUserInteractionEnabled = false
headerBgImage.layoutChain
.top()
.edgesHorzontal()
.heightToWidth(120 / 375)
let titleLab = UILabel()
titleLab.text = "性别"
titleLab.font = .systemFont(ofSize: 18, weight: .semibold)
titleLab.textColor = UIColor(hexStr: "#293445")
contentView.addSubview(titleLab)
titleLab.layoutChain.top(22).centerX()
let closeBtn = UIButton(type: .custom)
closeBtn.setImage(UIImage(named: "MemberInfo/close_circle"), for: .normal)
closeBtn.addTarget(self, action: #selector(close), for: .touchUpInside)
contentView.addSubview(closeBtn)
closeBtn.layoutChain.right(16).centerY(titleLab).width(28).height(28)
let row = UIStackView()
row.axis = .horizontal
row.alignment = .fill
row.distribution = .fillEqually
row.spacing = 16
contentView.addSubview(row)
row.layoutChain
.topToBottomOfView(titleLab, offset: 24)
.left(28)
.right(28)
.height(100)
for (index, option) in options.enumerated() {
let item = makeOption(option, tag: index)
optionButtons.append(item.button)
row.addArrangedSubview(item.wrap)
}
contentView.bringSubviewToFront(row)
let confirm = UIButton(type: .custom)
confirm.setTitle("确 定", for: .normal)
confirm.setTitleColor(.white, for: .normal)
confirm.titleLabel?.font = FontManager.boboBold(18)
confirm.setBackgroundImage(UIImage(named: "Common/button_bg_2"), for: .normal)
confirm.layer.cornerRadius = 20
confirm.clipsToBounds = true
confirm.addTarget(self, action: #selector(confirmTapped), for: .touchUpInside)
contentView.addSubview(confirm)
confirm.layoutChain
.topToBottomOfView(row, offset: 22)
.left(30)
.right(30)
.height(50)
.bottom(16 + kSafeBottomMargin)
let height = 22 + 28 + 24 + 100 + 22 + 50 + 16 + kSafeBottomMargin
contentBgView.layoutChain.height(height)
contentView.layoutChain.height(height)
view.layoutIfNeeded()
}
private func makeOption(
_ option: (sex: Int, title: String, on: String, off: String),
tag: Int
) -> (wrap: UIView, button: UIButton) {
let wrap = UIControl()
wrap.tag = tag
wrap.addTarget(self, action: #selector(tapOption(_:)), for: .touchUpInside)
let button = UIButton(type: .custom)
button.isUserInteractionEnabled = false
button.tag = tag
button.backgroundColor = UIColor(hexStr: "#F5F6F8")
button.layer.cornerRadius = 16
button.clipsToBounds = true
wrap.addSubview(button)
button.layoutChain.top().centerX().width(72).height(72)
let lab = UILabel()
lab.text = option.title
lab.font = .systemFont(ofSize: 14, weight: .medium)
lab.textColor = UIColor(hexStr: "#293445")
lab.textAlignment = .center
wrap.addSubview(lab)
lab.layoutChain.topToBottomOfView(button, offset: 8).centerX()
return (wrap, button)
}
private func refreshSelection() {
for (index, button) in optionButtons.enumerated() {
let option = options[index]
let selected = selectedSex == option.sex
button.layer.borderWidth = selected ? 1.5 : 0
button.layer.borderColor = UIColor(hexStr: "#16B3FF").cgColor
let name = selected ? option.on : option.off
button.setImage(UIImage(named: name), for: .normal)
}
}
@objc private func tapOption(_ sender: UIControl) {
guard options.indices.contains(sender.tag) else { return }
selectedSex = options[sender.tag].sex
refreshSelection()
}
@objc private func confirmTapped() {
let sex = selectedSex
dismiss(animated: true) { [weak self] in
self?.onConfirm?(sex)
}
}
@objc private func close() {
dismiss(animated: true)
}
}
private final class GenderPopGradientView: UIView {
override class var layerClass: AnyClass { CAGradientLayer.self }
override init(frame: CGRect) {
super.init(frame: frame)
isUserInteractionEnabled = false
guard let gradient = layer as? CAGradientLayer else { return }
gradient.colors = [
UIColor(hexStr: "#BAF4FF").cgColor,
UIColor(hexStr: "#BAF4FF", alpha: 0).cgColor
]
gradient.startPoint = CGPoint(x: 0.5, y: 0)
gradient.endPoint = CGPoint(x: 0.5, y: 1)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}