Blame view

Twear/Basic/View/DatePickerView.swift 5.22 KB
75d24c15   yangbin   123
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
158
159
160
161
162
163
164
165
166
167
168
169
  //
  //  DatePickerView.swift
  //  Twear
  //
  //  Created by yangbin on 2021/12/24.
  //
  
  import UIKit
  
  class DatePickerView: UIView {
      
      typealias clickAlertClosure = (_ date: Date) -> Void
      var clickClosure: clickAlertClosure!
      
      private let bgView = UIView() //白色框
      
      private lazy var titleLabel: UILabel = {
          let label = UILabel()
          label.font = RegularFont(15)
          label.textColor = .black
          label.textAlignment = .center
          return label
      }()
      
  //    private let minuteArray: [Int] = {
  //        var array: [Int] = []
  //        for i in 0..<50 {
  //            array.append((i+1)*1000)
  //        }
  //        return array
  //    }()
      private lazy var sureBtn: UIButton = {
          let button = UIButton()
          button.addTarget(self, action: #selector(clickBtnAction(_:)), for: .touchUpInside)
  //        button.setTitleColor(UIColor.rgbColorFromHex(0x00993E), for: .normal)
          button.setTitle(LocString("确定"), for: .normal)
          button.titleLabel?.font = RegularFont(11)
          button.tag = 1
          return button
      }()
      
      private lazy var cancelBtn: UIButton = {
          let button = UIButton()
          button.addTarget(self, action: #selector(clickBtnAction(_:)), for: .touchUpInside)
          button.setTitleColor(UIColor.rgbColorFromHex(0x808080), for: .normal)
          button.setTitle(LocString("取消"), for: .normal)
          button.titleLabel?.font = RegularFont(11)
          button.tag = 2
          return button
      }()
  
      lazy var datePicker: UIDatePicker = {
          let picker = UIDatePicker()
          picker.datePickerMode = .time
          picker.locale = .current
          if #available(iOS 13.4, *) {
              picker.preferredDatePickerStyle = .wheels
          } else {
              
          }
          return picker
      }()
      
      init(title: String, sureText: String = LocString("确定"), cancelText: String = LocString("取消"), titleColor: UIColor = UIColor.rgbColorFromHex(0x00993E)) {
          super.init(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT))
          createView(title: title, sureText: sureText, cancelText: cancelText, titleColor: titleColor)
      }
      
  
      func createView(title: String, sureText: String, cancelText: String, titleColor: UIColor) {
          self.backgroundColor = UIColor.black.withAlphaComponent(0.1)
          
          bgView.backgroundColor = .white
          
  //        self.titleColor = titleColor
          titleLabel.text = title
          sureBtn.setTitle(sureText, for: .normal)
          cancelBtn.setTitle(cancelText, for: .normal)
          sureBtn.setTitleColor(titleColor, for: .normal)
          
          let lineView = UIView()
          lineView.backgroundColor = LineColor
          addSubview(bgView)
          bgView.addSubview(titleLabel)
          bgView.addSubview(sureBtn)
          bgView.addSubview(cancelBtn)
          bgView.addSubview(lineView)
          bgView.addSubview(datePicker)
          
          bgView.snp.makeConstraints { (make) in
              make.left.right.bottom.equalToSuperview()
              make.height.equalTo(250)
          }
          cancelBtn.snp.makeConstraints { make in
              make.height.equalTo(40)
              make.width.equalTo(65)
              make.left.top.equalToSuperview()
          }
          sureBtn.snp.makeConstraints { make in
              make.height.equalTo(40)
              make.width.equalTo(65)
              make.right.top.equalToSuperview()
          }
          titleLabel.snp.makeConstraints { make in
              make.centerX.equalToSuperview()
              make.centerY.equalTo(cancelBtn)
          }
          lineView.snp.makeConstraints { make in
              make.left.right.equalToSuperview()
              make.top.equalToSuperview().offset(40)
              make.height.equalTo(0.5)
          }
          datePicker.snp.makeConstraints { make in
              make.top.equalTo(lineView)
              make.left.right.bottom.equalToSuperview()
          }
          
          
          bgView.layoutIfNeeded()
          bgView.setCorners(corners: [.topLeft, .topRight], radio: 15)
      }
      
  //    @objc func dateChanged(datePicker : UIDatePicker){
  //        let formatter = DateFormatter()
  //        //日期样式
  //        formatter.dateFormat = "yyyy年MM月dd日 HH:mm:ss"
  //        print(formatter.string(from: datePicker.date))
  //    }
      
      @objc func clickBtnAction(_ sender: UIButton) {
          if clickClosure != nil && sender.tag == 1 {
              clickClosure!(datePicker.date)
          }
          dismiss()
      }
      
      func show() {
          let wind = KeyWindow
          self.alpha = 0
          
          wind.addSubview(self)
          UIView.animate(withDuration: 0.25, animations: { () -> Void in
              self.alpha = 1
          })
      }
      
      @objc func dismiss() {
          UIView.animate(withDuration: 0.25, animations: { () -> Void in
              self.alpha = 0
          }, completion: { (finish) -> Void in
              if finish {
                  self.removeFromSuperview()
              }
          })
      }
      
      required init?(coder: NSCoder) {
          fatalError("init(coder:) has not been implemented")
      }
      
      /*
      // Only override draw() if you perform custom drawing.
      // An empty implementation adversely affects performance during animation.
      override func draw(_ rect: CGRect) {
          // Drawing code
      }
      */
  
  }