宽窄优行-由【嘉易行】项目成品而来
younger_times
2023-07-05 0d8f5fc8a516bfd07e425909e4a4432600572ee7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//
//  YYQuestionAlertController.swift
//  FollowMeProject
//
//  Created by alvin_y on 2020/9/7.
//  Copyright © 2020 yangwang. All rights reserved.
//
 
import UIKit
 
typealias YYBottomOnDoneBlock = (String) -> Void
 
class YYBottomAlertController: YYViewController {
    @IBOutlet weak var tableViewHeightConstraint: NSLayoutConstraint!
    
    @IBOutlet weak var view_title: UIView!
    @IBOutlet weak var label_title: UILabel!
    /// UIView
    @IBOutlet weak var view_container: UIView!
    
    /// UITableView
    @IBOutlet weak var tableView: UITableView!
    
    /// 数据源
    var dataSource: [String] = []
    var alertTitle: String?
    
    var onDone: YYBottomOnDoneBlock?
    
    
    init (){
        super.init(nibName: String(describing: YYBottomAlertController.self), bundle: Bundle.main)
        
        modalTransitionStyle = .crossDissolve
        modalPresentationStyle = .overFullScreen
        
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    class func display(dataSource: [String],onDone: @escaping (String) -> Void) {
        let vc = YYBottomAlertController()
        vc.onDone = onDone
        vc.dataSource = dataSource
        self.base()?.present(vc, animated: true, completion: nil)
    }
    
    class func display(title: String,dataSource: [String],onDone: @escaping (String) -> Void) {
        let vc = YYBottomAlertController()
        vc.onDone = onDone
        vc.alertTitle = title
        vc.dataSource = dataSource
        self.base()?.present(vc, animated: true, completion: nil)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        tableView.reloadData()
        view_container.transform = CGAffineTransform.init(translationX: 0, y: view_container.maxY + 50)
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        UIView.animate(withDuration: 0.2) {
            self.view_container.transform = CGAffineTransform.identity
        }
    }
    
    /// 隐藏
    func dismiss() {
        UIView.animate(withDuration: 0.2, animations: {
            self.view_container.transform = CGAffineTransform.init(translationX: 0, y: self.view_container.maxY + 50)
        }) { (_) in
            self.dismiss(animated: false, completion: nil)
        }
    }
    
    //MARK: - UI
    override func setupViews() {
        super.setupViews()
        view.backgroundColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0.65)
        
        view_container.addRoundedCorners(corners: [.topLeft,.topRight], rect: CGRect(x: 0, y: 0, width: screenW, height: CGFloat(MAXFLOAT)), radius: CGSize(width: 10, height: 10))
        
        tableView.delegate = self
        tableView.dataSource = self
        tableView.separatorStyle = .none
        tableView.register(cellWithClass: UITableViewCell.self)
        label_title.text = alertTitle
        view_title.isHidden = alertTitle == nil
    }
    
    //MARK: - Rx
    override func bindRx() {
        super.bindRx()
        tableView.rx.observe(CGSize.self, #keyPath(UITableView.contentSize))
        .subscribe(onNext: {[unowned self] (size) in
            guard let height = size?.height else{return}
            self.tableViewHeightConstraint.constant = height
        })
        .disposed(by: rx.disposeBag)
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        var point: CGPoint? = touches.first?.location(in: view)
        point = view_container.layer.convert(point ?? CGPoint.zero, from: view.layer)
        if view_container.layer.contains(point ?? CGPoint.zero) {
            //处理点击到这个view中要执行的事件
            return
        }
        super.touchesBegan(touches, with: event)
        dismiss()
    }
 
}
 
 
// MARK: - UITableViewDelegate
extension YYBottomAlertController:UITableViewDelegate{
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 54
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        dismiss(animated: true) {
            self.onDone?(self.dataSource[indexPath.row])
        }
    }
}
 
// MARK: - UITableViewDelegate
extension YYBottomAlertController:UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataSource.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withClass: UITableViewCell.self, for: indexPath)
        cell.textLabel?.textAlignment = .center
        cell.textLabel?.text = dataSource[indexPath.row]
        switch dataSource[indexPath.row] {
        case "取消":
            cell.textLabel?.font = UIFont.systemFont(ofSize: 14, weight: .regular)
            cell.textLabel?.textColor = #colorLiteral(red: 0.6901960784, green: 0.6901960784, blue: 0.6901960784, alpha: 1)
        default:
            cell.textLabel?.font = UIFont.systemFont(ofSize: 14, weight: .medium)
            cell.textLabel?.textColor = #colorLiteral(red: 0.2, green: 0.2, blue: 0.2, alpha: 1)
        }
        return cell
    }
    
}