SBTRTStatusCell.swift 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. //
  2. // SBTRTStatusCell.swift
  3. // SolarBT
  4. //
  5. // Created by weclouds on 2019/1/28.
  6. // Copyright © 2019 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. class RealTimeItem: UIView {
  10. var valueString: String?{
  11. didSet{
  12. if let valueString = valueString {
  13. let strLength = valueString.count
  14. let attrStr = NSMutableAttributedString(string: valueString)
  15. let strfont : UIFont = UIFont.boldSystemFont(ofSize: 10)
  16. let attr2: [NSAttributedString.Key : Any] = [NSAttributedString.Key.font: strfont,NSAttributedString.Key.foregroundColor: UIColor(red: 0.53, green: 0.53, blue: 0.53, alpha: 1)]
  17. attrStr.addAttributes(attr2, range: NSRange(location: strLength - 1, length: 1))
  18. valueLabel.attributedText = attrStr
  19. }
  20. }
  21. }
  22. var title:String?{
  23. didSet{
  24. titleLabel.text = title
  25. }
  26. }
  27. var image : UIImage?{
  28. didSet{
  29. imageView.image = image
  30. }
  31. }
  32. lazy var titleLabel: UILabel = {
  33. let titleLabel = UILabel()
  34. titleLabel.font = UIFont(name: "苹方 中等", size: 12)
  35. titleLabel.textColor = UIColor(hexString: "222222")
  36. return titleLabel
  37. }()
  38. lazy var imageView: UIImageView = {
  39. let imageView = UIImageView()
  40. imageView.contentMode = UIView.ContentMode.scaleAspectFit
  41. return imageView
  42. }()
  43. lazy var valueLabel: UILabel = {
  44. let valueLabel = UILabel()
  45. // valueLabel.textAlignment = .center
  46. valueLabel.font = UIFont(name: "苹方 粗体", size: 18)
  47. valueLabel.textColor = UIColor(hexString: "222222")
  48. return valueLabel
  49. }()
  50. //ff0302009091fc
  51. override init(frame: CGRect) {
  52. super.init(frame: frame)
  53. createUI()
  54. }
  55. required init?(coder aDecoder: NSCoder) {
  56. fatalError("init(coder:) has not been implemented")
  57. }
  58. func createUI() {
  59. //设置阴影
  60. addSubview(titleLabel)
  61. addSubview(imageView)
  62. addSubview(valueLabel)
  63. titleLabel.snp.makeConstraints { (make) in
  64. make.left.equalToSuperview().offset(15)
  65. make.top.equalToSuperview().offset(14)
  66. }
  67. imageView.snp.makeConstraints { (make) in
  68. make.right.equalToSuperview().offset(-14)
  69. make.top.equalToSuperview().offset(14)
  70. make.height.width.equalTo(14)
  71. }
  72. valueLabel.snp.makeConstraints { (make) in
  73. make.right.equalToSuperview().offset(0)
  74. make.left.equalToSuperview().offset(15)
  75. make.bottom.equalToSuperview().offset(-8)
  76. }
  77. self.layer.masksToBounds = true
  78. self.layer.cornerRadius = 4
  79. }
  80. }
  81. class SBTRTStatusCell: UITableViewCell,ZQTCustomSwitchDelegate {
  82. var isDemo :Bool?
  83. var valueModel: RealTimeValue?{
  84. didSet{
  85. item?.valueString = self.valueModel?.itemValue
  86. item1?.valueString = self.valueModel?.itemValue2
  87. item2?.valueString = self.valueModel?.itemValue3
  88. self.statusLabel.text = self.valueModel?.status
  89. if self.valueModel?.switchValue == "1" { // 逻辑开 -- > UI开
  90. self.aSwitch.isOn = true
  91. aSwitch.thumbTintColor = UIColor(hexString: "29B009")
  92. }else if self.valueModel?.switchValue == "0"{ //逻辑关 // UI关
  93. self.aSwitch.isOn = false
  94. aSwitch.thumbTintColor = UIColor(hexString: "A3A3A3")
  95. }
  96. }
  97. }
  98. var titleModel:ReatTimeName?{
  99. didSet{
  100. titleLabel.text = self.titleModel?.title
  101. item?.title = self.titleModel?.itemName1
  102. item1?.title = self.titleModel?.itemName2
  103. item2?.title = self.titleModel?.itemName3
  104. item?.image = UIImage(named:self.titleModel!.itemName1! )
  105. item1?.image = UIImage(named:self.titleModel!.itemName2!)
  106. item2?.image = UIImage(named:self.titleModel!.itemName3! )
  107. }
  108. }
  109. var item : RealTimeItem?
  110. var item1 :RealTimeItem?
  111. var item2 :RealTimeItem?
  112. @IBOutlet weak var titleLabel: UILabel!
  113. @IBOutlet weak var statusLabel: UILabel!
  114. var switchCallback : ((Bool)->Void)?
  115. var isSwitchHidden : Bool = true{
  116. didSet{
  117. self.aSwitch.isHidden = self.isSwitchHidden
  118. }
  119. }
  120. var isSwitchEnabled:Bool = false{
  121. didSet{
  122. self.aSwitch.isEnabled = self.isSwitchEnabled
  123. }
  124. }
  125. lazy var aSwitch :ZQTCustomSwitch = {
  126. let aSwift = ZQTCustomSwitch(frame: CGRect.zero, on: UIColor(hexString: "#F2F2F2"), offColor: UIColor(hexString: "#F2F2F2"), font: UIFont(name: PingFangSC_Medium, size: 9), ballSize: 17)
  127. aSwift?.onText = "On".da_localizedStr()
  128. aSwift?.offText = "Off".da_localizedStr()
  129. aSwift?.textColor = UIColor(hexString: "BFBFBF")
  130. aSwift?.thumbTintColor = UIColor(hexString: "29B009")
  131. aSwift?.isHidden = self.isSwitchHidden
  132. aSwift?.delegate = self
  133. return aSwift!
  134. }()
  135. @objc func switchPressed(_ aswitch: ZQTCustomSwitch) {
  136. // aswitch.isEnabled = false
  137. // if aswitch.isOn == true {
  138. // aSwitch.thumbTintColor = UIColor(hexString: "29B009")
  139. // }else{
  140. // aSwitch.thumbTintColor = UIColor(hexString: "A3A3A3")
  141. // }
  142. //
  143. // if self.isDemo == true {
  144. // if aswitch.isOn == true{
  145. // statusLabel.text = "Load_is_on".da_localizedStr()
  146. // }else{
  147. // statusLabel.text = "Load_is_off".da_localizedStr()
  148. // }
  149. // }
  150. }
  151. override func awakeFromNib() {
  152. super.awakeFromNib()
  153. // Initialization code
  154. createUI()
  155. aSwitch.addTarget(self, action: #selector(switchPressed), for: .valueChanged)
  156. }
  157. override func setSelected(_ selected: Bool, animated: Bool) {
  158. super.setSelected(selected, animated: animated)
  159. // Configure the view for the selected state
  160. }
  161. func createUI() {
  162. addSubview(aSwitch)
  163. aSwitch.snp.makeConstraints { (make) in
  164. make.top.equalToSuperview().offset(10)
  165. make.left.equalTo(self.titleLabel.snp.right).offset(15)
  166. make.width.equalTo(45)
  167. make.height.equalTo(22)
  168. }
  169. let itemWidth = (KSCREENWIDTH - 18 - 18 - 10 - 10) / 3
  170. let itemHeight = 61.0
  171. item = RealTimeItem(frame: CGRect(x: 18.0, y: 10.0 + 20 + 19 , width: Double(itemWidth), height: itemHeight))
  172. item?.valueString = "97.6V"
  173. item1 = RealTimeItem(frame: CGRect(x: Double(18.0 + 10 + itemWidth), y: 10.0 + 20 + 19 , width: Double(itemWidth), height: itemHeight))
  174. item1?.valueString = "271A"
  175. item2 = RealTimeItem(frame: CGRect(x: Double(18.0 + 10 + 10 + itemWidth + itemWidth), y: 10.0 + 20 + 19 , width: Double(itemWidth), height: itemHeight))
  176. item2?.valueString = "271W"
  177. self.contentView.addSubview(item!)
  178. self.contentView.addSubview(item1!)
  179. self.contentView.addSubview(item2!)
  180. item!.setShadow( UIColor(red: 0.84, green: 0.84, blue: 0.84, alpha: 0.33), shadowOffset: CGSize(width: 0, height: 2), shadowOpacity: 0.1, shadowRadius: 4)
  181. item1!.setShadow( UIColor(red: 0.84, green: 0.84, blue: 0.84, alpha: 0.33), shadowOffset: CGSize(width: 0, height: 2), shadowOpacity: 0.1, shadowRadius: 4)
  182. item2!.setShadow( UIColor(red: 0.84, green: 0.84, blue: 0.84, alpha: 0.33), shadowOffset: CGSize(width: 0, height: 2), shadowOpacity: 0.1, shadowRadius: 4)
  183. }
  184. func handleGestureRecognizerWithSwith(on isOn: Bool) {
  185. self.switchCallback!(isOn)
  186. }
  187. }