宽窄优行-由【嘉易行】项目成品而来
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
//
//  MineInfoView.swift
//  OKProject
//
//  Created by alvin_y on 2020/6/13.
//  Copyright © 2020 yangwang. All rights reserved.
//
 
import UIKit
 
typealias MineInfoBlock = (Int) -> ()
 
class MineInfoView: UIView {
    
    /// UIPickerView
    @IBOutlet weak var pickerView: UIPickerView!
    
    /// 取消
    @IBOutlet weak var button_cancel: UIButton!
    
    /// 提交
    @IBOutlet weak var button_submit: UIButton!
    
    /// 容器
    @IBOutlet weak var view_container: YYView!
    
    /// 数据源
    private var dataSource: [String] = []
    
    /// 成功
    var mineInBlock: MineInfoBlock?
    
    /// 获取Self
    /// - Returns: Self
    class func instance(data: [String]) -> MineInfoView {
        let v = UINib(nibName: "MineInfoView", bundle: nil).instantiate(withOwner: self, options: nil).first as! MineInfoView
        v.setupViews()
        v.bindRx()
        v.dataSource = data
        return v
    }
    
    //MARK: - UI
    func setupViews() {
        self.frame = UIScreen.main.bounds
        self.view_container.transform = CGAffineTransform.init(translationX: 0, y: screenH)
        self.backgroundColor = UIColor.color(light: UIColor.color(hexString: "#000000",0.5), dark: UIColor.color(hexString: "#000000",0.61))
        
        pickerView.delegate = self
        pickerView.dataSource = self
        pickerView.reloadAllComponents()
    }
 
    
    //MARK: - Rx
    func bindRx() {
        button_submit.rx.tap.subscribe(onNext: {[unowned self] (_) in
            let row = self.pickerView.selectedRow(inComponent: 0)
            self.dismiss()
            self.mineInBlock?(row)
        }).disposed(by: rx.disposeBag)
        
        button_cancel.rx.tap.subscribe(onNext: {[unowned self] (_) in
            self.dismiss()
        }).disposed(by: rx.disposeBag)
    }
    
    /// 显示
    func show(success: @escaping (Int) -> ())  {
        self.mineInBlock = success
        app.window?.addSubview(self)
        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: screenH)
        }) { (_) in
            self.removeFromSuperview()
        }
        
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        var point: CGPoint? = touches.first?.location(in: self)
        point = view_container.layer.convert(point ?? CGPoint.zero, from: self.layer)
        if view_container.layer.contains(point ?? CGPoint.zero) {
            //处理点击到这个view中要执行的事件
            return
        }
        super.touchesBegan(touches, with: event)
        self.dismiss()
    }
}
// MARK: - UIPickerViewDelegate
extension MineInfoView:UIPickerViewDelegate,UIPickerViewDataSource{
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return dataSource.count
    }
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        //时间表的线
        for singleLine in pickerView.subviews {
            if singleLine.frame.size.height < 2{
                singleLine.isHidden = true
            }
        }
        
        let label:UILabel = view as? UILabel ?? UILabel()
        label.font = UIFont.boldSystemFont(ofSize: 16)
        label.textAlignment = .center
        label.adjustsFontSizeToFitWidth = true
        label.text = dataSource[row]
        return label
    }
    
    
    func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
        return screenW - 28
    }
    
    func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return 25
    }
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    }
}