//
|
// 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!
|
@IBOutlet weak var cons_titleH: NSLayoutConstraint!
|
@IBOutlet weak var view_title: UIView!
|
|
private var items = [String]()
|
private var clouse:((Int)->Void)!
|
private var hiddenTitle:Bool = false
|
|
override func awakeFromNib() {
|
super.awakeFromNib()
|
alpha = 0
|
|
cons_viewBottom.constant = -JQ_ScreenW
|
layoutIfNeeded()
|
tableView.delegate = self
|
tableView.dataSource = self
|
tableView.separatorStyle = .none
|
tableView.isScrollEnabled = false
|
tableView.register(CommonAlertSheetTCell.self, forCellReuseIdentifier: "TCell")
|
}
|
|
static func show(items:[String],hiddenTitle:Bool = false,clouse:@escaping (Int)->Void){
|
let alertView = CommonAlertSheetView.jq_loadNibView()
|
alertView.hiddenTitle = hiddenTitle
|
alertView.cons_titleH.constant = hiddenTitle ? 0:56
|
alertView.view_title.isHidden = hiddenTitle
|
|
alertView.frame = sceneDelegate?.window?.frame ?? .zero
|
alertView.cons_tableHei.constant = CGFloat(items.count * 56)
|
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()
|
var totalH:Double = 0
|
if hiddenTitle{
|
totalH = Double(items.count * 56) + UIDevice.jq_safeEdges.bottom + 56.0
|
}else{
|
totalH = Double(items.count * 56) + UIDevice.jq_safeEdges.bottom + 56.0 + 56.0
|
}
|
|
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]
|
if hiddenTitle{
|
cell.label_content.textColor = UIColor(hexString: "#6B6B6B")
|
}else{
|
cell.label_content.textColor = UIColor(hexString: "#92A87D")
|
}
|
return cell
|
}
|
|
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
|
return 56
|
}
|
}
|
|
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: 15, weight: .medium)
|
label_content.textColor = UIColor(hexString: "#6B6B6B")
|
|
contentView.addSubview(label_content)
|
label_content.snp.makeConstraints { make in
|
make.center.equalToSuperview()
|
}
|
|
let lineView = UIView()
|
lineView.backgroundColor = UIColor(hexStr: "#979797").withAlphaComponent(0.1)
|
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")
|
}
|
}
|