Blame view

Twear/Basic/View/TemperatureView.swift 4.03 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
  //
  //  TemperatureView.swift
  //  Twear
  //
  //  Created by yangbin on 2022/1/8.
  //
  
  import UIKit
  
  class TemperatureView: UIView {
      
      typealias clickAlertClosure = (_ value: String) -> Void
      var clickClosure: clickAlertClosure!
      
      @IBOutlet weak var textLabel: UILabel!
      @IBOutlet private weak var bgView: UIView!
      
      var temperature: Float = 0
      
      private var doInput: Bool = false
      private var text: String = "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("TemperatureView", owner: self, options: nil)?.first as? UIView {
              view.frame = bounds
              self.addSubview(view)
          }
      }
  
      init() {
          super.init(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT))
  //        self.shareImage = image
          initFromNib()
          setupUI()
      }
      
      func setupUI() {
          bgView.layoutIfNeeded()
          bgView.setCorners(corners: [.topLeft, .topRight], radio: 15)
          textLabel.toUnitMode(text: "0", unit: " ℃", font: BoldFont(25), unitFont: BoldFont(13))
      }
      
      
      @IBAction func clickBtnAction(_ sender: UIButton) {
  //        if (clickClosure != nil) {
  //            clickClosure!(sender.tag)
  //        }
          switch sender.tag {
          case 0..<10:
              let num = sender.tag
              let oldText = text
              let array = text.split(separator: ".")
              if array.count == 2 {
                  if array[0].count == 1 && text.count >= 4 {
                      return
                  }
              } else {
                  if text.count >= 5 {
                      return
                  }
              }
              if doInput {
                  text += "\(num)"
              } else {
                  if text == "0" {
                      text = "\(num)"
                  } else {
                      text += "\(num)"
                  }
              }
              print(text)
              if let result = Float(text), result < 100 {
                  textLabel.toUnitMode(text: text, unit: " ℃", font: BoldFont(25), unitFont: BoldFont(13))
              } else {
                  text = oldText
                  textLabel.toUnitMode(text: oldText, unit: " ℃", font: BoldFont(25), unitFont: BoldFont(13))
              }
              
          case 10:
              if !doInput {
                  text += "."
                  doInput = true
              }
  //            doInput = !doInput
              textLabel.toUnitMode(text: text, unit: " ℃", font: BoldFont(25), unitFont: BoldFont(13))
          case 20:
              if text.substring(fromIndex: text.count-1) == "." {
                  doInput = false
              }
              if text.count == 1 {
                  text = "0"
              } else {
                  text = text.substring(toIndex: text.count-1)
              }
              textLabel.toUnitMode(text: text, unit: " ℃", font: BoldFont(25), unitFont: BoldFont(13))
          case 30:
              dismiss()
          case 31:
              if let result = Float(text), result < 100, result > 0 {
                  if (clickClosure != nil) {
                      clickClosure!(textLabel.text ?? "--℃")
                  }
              }
      
              dismiss()
          default:
              break
          }
      }
      
      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() {
  //        MBProgressHUD.hide()
          UIView.animate(withDuration: 0.25, animations: { () -> Void in
              self.alpha = 0
          }, completion: { (finish) -> Void in
              if finish {
                  self.removeFromSuperview()
              }
          })
      }
      
      deinit {
          print("deinit\(NSStringFromClass(type(of: self)))!!!!!!!")
      }
  }