//
|
// CommonAlertSheetView.swift
|
// WanPai
|
//
|
// Created by 无故事王国 on 2023/10/9.
|
//
|
|
import UIKit
|
import JQTools
|
|
class CommonAlertSheetView: UIView,JQNibView {
|
|
@IBOutlet weak var view_container: UIView!
|
@IBOutlet weak var tableView: UITableView!
|
@IBOutlet weak var cons_tableHei: NSLayoutConstraint!
|
@IBOutlet weak var cons_viewBottom: NSLayoutConstraint!
|
|
private var items = [String]()
|
private var clouse:((Int)->Void)!
|
|
override func awakeFromNib() {
|
super.awakeFromNib()
|
alpha = 0
|
cons_viewBottom.constant = -JQ_ScreenW
|
layoutIfNeeded()
|
tableView.delegate = self
|
tableView.dataSource = self
|
tableView.separatorStyle = .none
|
tableView.register(CommonAlertSheetTCell.self, forCellReuseIdentifier: "TCell")
|
}
|
|
static func show(items:[String],clouse:@escaping (Int)->Void){
|
let alertView = CommonAlertSheetView.jq_loadNibView()
|
alertView.frame = sceneDelegate?.window?.frame ?? .zero
|
alertView.cons_tableHei.constant = CGFloat(items.count * 50)
|
sceneDelegate?.window?.addSubview(alertView)
|
alertView.cons_viewBottom.constant = 0
|
alertView.items = items
|
alertView.clouse = clouse
|
|
UIView.animate(withDuration: 0.4) {
|
alertView.layoutIfNeeded()
|
alertView.alpha = 1
|
} completion: { _ in
|
|
}
|
}
|
|
override func layoutSubviews() {
|
super.layoutSubviews()
|
let totalH = Double(items.count * 50) + UIDevice.jq_safeEdges.bottom + 50
|
view_container.jq_addCorners(corner: [.topLeft,.topRight], radius: 20,height: totalH)
|
}
|
|
private func hidden(){
|
self.cons_viewBottom.constant = -JQ_ScreenW
|
UIView.animate(withDuration: 0.4) {
|
self.alpha = 0
|
self.layoutIfNeeded()
|
} completion: { _ in
|
self.removeFromSuperview()
|
}
|
}
|
|
@IBAction func hiddenAction(_ sender: UIButton) {
|
hidden()
|
}
|
}
|
|
extension CommonAlertSheetView:UITableViewDelegate{
|
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
clouse(indexPath.row)
|
hidden()
|
}
|
}
|
|
extension CommonAlertSheetView:UITableViewDataSource{
|
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
return items.count
|
}
|
|
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
let cell = tableView.dequeueReusableCell(withIdentifier: "TCell") as! CommonAlertSheetTCell
|
cell.label_content.text = items[indexPath.row]
|
return cell
|
}
|
|
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
|
return 50
|
}
|
}
|
|
fileprivate class CommonAlertSheetTCell:UITableViewCell{
|
|
var label_content:UILabel!
|
|
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
|
super.init(style: style, reuseIdentifier: reuseIdentifier)
|
|
selectionStyle = .none
|
label_content = UILabel()
|
label_content.text = "123"
|
label_content.font = UIFont.systemFont(ofSize: 16, weight: .medium)
|
label_content.textColor = .black.withAlphaComponent(0.8)
|
|
contentView.addSubview(label_content)
|
label_content.snp.makeConstraints { make in
|
make.center.equalToSuperview()
|
}
|
|
let lineView = UIView()
|
lineView.backgroundColor = UIColor(hexStr: "#DBDBE7")
|
contentView.addSubview(lineView)
|
lineView.snp.makeConstraints { make in
|
make.left.equalTo(15)
|
make.right.equalTo(-15)
|
make.height.equalTo(0.5)
|
make.bottom.equalToSuperview()
|
}
|
}
|
|
required init?(coder: NSCoder) {
|
fatalError("init(coder:) has not been implemented")
|
}
|
}
|