Blame view

HDFwear/Basic/View/DateSegmentView.swift 4.26 KB
f2cf74c7   yangbin   1.0.20(4)
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
  //
  //  DateSegmentView.swift
  //  Twear
  //
  //  Created by yangbin on 2021/11/19.
  //
  
  import UIKit
  import SwiftDate
  
  
  enum DateType : Int {
      case day = 0
      case week
      case month
      case year
  }
  
  protocol DateSegmentViewDelegate: NSObjectProtocol {
      func didSelectedDate(date: DateInRegion, dateType: DateType)
  }
  
  class DateSegmentView: UIView {
      
      @IBOutlet weak var dayLayout: NSLayoutConstraint!
      @IBOutlet weak var dateTypeView: UIView!
      var normalColor: UIColor = .black
      var selectedColor: UIColor = .blue {
          didSet {
              dayButton.setTitleColor(selectedColor, for: .normal)
          }
      }
      
      @IBOutlet weak var dateLabel: UILabel!
      @IBOutlet private weak var monthButton: UIButton!
      @IBOutlet private weak var dayButton: UIButton!
      @IBOutlet private weak var yearButton: UIButton!
      @IBOutlet weak var weekButton: UIButton!
      
      weak var delegate: DateSegmentViewDelegate?
      private let date = DateInRegion()
      var dateType: DateType = .day
      private var dayCount: Int = 0
      private var weekCount: Int = 0
      private var monthCount: Int = 0
      private var yearCount: Int = 0
      
      override init(frame: CGRect) {
          super.init(frame: frame)
          initFromNib()
          setupUI()
      }
      required init?(coder: NSCoder) {
          super.init(coder: coder)
          initFromNib()
          setupUI()
      }
      
      private func initFromNib() {
          if let view = Bundle.main.loadNibNamed("DateSegmentView", owner: self, options: nil)?.first as? UIView {
              view.frame = bounds
              self.addSubview(view)
          }
      }
      
      
      
      
      @IBAction func clickLeftButton(_ sender: Any) {
          updateDate(-1)
      }
      @IBAction func clickRightButton(_ sender: Any) {
          updateDate(1)
      }
      func updateDate(_ count: Int = 0) {
          var selectedDate = DateInRegion()
          switch dateType {
          case .day:
              let newDate = date + (dayCount+count).days
              if newDate.isInFuture {
                  return
              }
              dayCount += count
              selectedDate = newDate
              dateLabel.text = newDate.toString(.custom("yyyy.MM.dd"))
          case .week:
              let newDate = (date-1.days).dateAt(.startOfWeek) + (weekCount+count).weeks + 1.days
              if newDate.isInFuture {
                  return
              }
              weekCount += count
              selectedDate = newDate
              let startDate = ((date-1.days).dateAt(.startOfWeek) + weekCount.weeks + 1.days).toString(.custom(
                  "yyyy.MM.dd"))
              let endDate = ((date-1.days).dateAt(.endOfWeek) + weekCount.weeks + 1.days).toString(.custom("yyyy.MM.dd"))
              dateLabel.text = "\(startDate)-\(endDate)"
          case .month:
              let newDate = date + (monthCount+count).months
              if newDate.isInFuture {
                  return
              }
              selectedDate = newDate
              monthCount += count
              dateLabel.text = newDate.toString(.custom("yyyy.MM"))
          case .year:
              let newDate = date + (yearCount+count).years
              if newDate.isInFuture {
                  return
              }
              selectedDate = newDate
              yearCount += count
              let year = newDate.toString(.custom("yyyy"))
              dateLabel.text = "\(year).01-\(year).12"
          }
          delegate?.didSelectedDate(date: selectedDate, dateType: dateType)
      }
      
  
      
      func setupUI() {
          dayButton.setTitleColor(selectedColor, for: .normal)
          dateLabel.text = date.toString(.custom("yyyy.MM.dd"))
      }
  
      
      @IBAction func selectedDate(_ sender: UIButton) {
          monthButton.setTitleColor(normalColor, for: .normal)
          dayButton.setTitleColor(normalColor, for: .normal)
          yearButton.setTitleColor(normalColor, for: .normal)
          weekButton.setTitleColor(normalColor, for: .normal)
          sender.setTitleColor(selectedColor, for: .normal)
          dateType = DateType(rawValue: sender.tag) ?? .day
  //        delegate?.didSelectedDate(dateType: dateType)
          updateDate()
      }
      
      
      
      /*
      // Only override draw() if you perform custom drawing.
      // An empty implementation adversely affects performance during animation.
      override func draw(_ rect: CGRect) {
          // Drawing code
      }
      */
  
  }