宽窄优行-由【嘉易行】项目成品而来
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
//
//  MineBirthdayView.swift
//  OKProject
//
//  Created by alvin_y on 2020/6/15.
//  Copyright © 2020 yangwang. All rights reserved.
//
 
import UIKit
 
class MineBirthdayView: UIView {
    
    /// viewModel
    let viewModel = MineInfoViewModel()
    
    /// UIDatePicker
    @IBOutlet weak var datePicker: UIDatePicker!
    
    /// 取消
    @IBOutlet weak var button_cancel: UIButton!
    
    /// 提交
    @IBOutlet weak var button_submit: UIButton!
    
    /// 容器
    @IBOutlet weak var view_container: YYView!
    
    typealias MineMineBirthdayBlock = (String) -> ()
    
    /// 成功
    var mineInBlock: MineMineBirthdayBlock?
    private var maximumDate:Date?
    
    /// 获取Self
    /// - Returns: Self
    class func instance() -> MineBirthdayView {
        let v = UINib(nibName: "MineBirthdayView", bundle: nil).instantiate(withOwner: self, options: nil).first as! MineBirthdayView
        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))
        
        datePicker.maximumDate = maximumDate
        if #available(iOS 13.4, *) {
            datePicker.preferredDatePickerStyle = .wheels
        } else {
            
        }
    }
 
    
    //MARK: - Rx
    func bindRx() {
        
        button_submit.rx.tap.subscribe(onNext: {[unowned self] (_) in
            let  chooseDate = self.datePicker.date
            let  dateFormater = DateFormatter.init()
            dateFormater.dateFormat = "yyyy-MM-dd"
            self.dismiss()
            self.mineInBlock?("\(dateFormater.string(from: chooseDate))")
        }).disposed(by: rx.disposeBag)
        
        button_cancel.rx.tap.subscribe(onNext: {[unowned self] (_) in
            self.dismiss()
        }).disposed(by: rx.disposeBag)
    }
    
    /// 显示
    func show(_ maximumDate:Date? = Date(), success: @escaping (String) -> ())  {
        self.mineInBlock = success
        self.maximumDate = maximumDate
        self.datePicker.maximumDate = maximumDate
        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()
    }
}