宽窄优行-由【嘉易行】项目成品而来
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
//
//  TipView.swift
//  OKProject
//
//  Created by alvin_y on 2020/6/2.
//  Copyright © 2020 yangwang. All rights reserved.
//
 
import UIKit
 
/// 小费
class TipView: UIView {
    
    /// UIPickerView
    @IBOutlet weak var pickerView: UIPickerView!
    
    /// 描述
    @IBOutlet weak var label_desc: UILabel!
    
    /// 标题
    @IBOutlet weak var label_title: UILabel!
    
    /// 取消
    @IBOutlet weak var button_cancel: YYButton!
    
    /// 确定
    @IBOutlet weak var button_submit: YYButton!
    
    /// 背景容器
    @IBOutlet weak var view_container: YYView!
    
    /// 数据源
    private let dataSource = ["不添加","1元","2元","3元","4元","5元","6元","7元","8元","9元","10元","11元","12元","13元","14元","15元","16元","17元","18元","19元","20元"]
    
    let complete = Delegate<String,Void>()
    
    /// 获取Self
    /// - Returns: Self
    class func instance() -> TipView {
        let v = UINib(nibName: "TipView", bundle: nil).instantiate(withOwner: self, options: nil).first as! TipView
        v.setupViews()
        v.bindRx()
        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))
        view_container.backgroundColor = UIColor.color(light: UIColor.color(hexString: "#FFFFFF"), dark: UIColor.color(hexString: "#232323"))
        button_submit.setTitleColor(ThemeColor, for: .normal)
        button_cancel.setTitleColor(UIColor.color(light: UIColor.color(hexString: "#666666"), dark: UIColor.color(hexString: "#8C8C8C")), for: .normal)
        label_title.textColor = UIColor.color(light: UIColor.color(hexString: "#000000",0.8), dark: UIColor.color(hexString: "#FFFFFF"))
        label_desc.textColor = UIColor.color(light: UIColor.color(hexString: "#000000",0.6), dark: UIColor.color(hexString: "#D3D3D3",0.6))
        
        pickerView.delegate = self
        pickerView.dataSource = self
        pickerView.reloadAllComponents()
    }
 
    
    //MARK: - Rx
    func bindRx() {
        button_submit.rx.tap.subscribe(onNext: {[unowned self] (_) in
            let string = self.dataSource[self.pickerView.selectedRow(inComponent: 0)]
            self.complete.call(string)
            self.dismiss()
        }).disposed(by: rx.disposeBag)
    }
    
    /// 显示
      func show()  {
          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 TipView: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 50
    }
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    }
}