SBTHomeTableViewHeader.swift 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. //
  2. // SBTHomeTableViewHeader.swift
  3. // SolarBT
  4. //
  5. // Created by weclouds on 2019/1/24.
  6. // Copyright © 2019 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. class DeviceTypeBtn: UIView {
  10. /*
  11. 1、创建UI
  12. 2、创建点击手势 tap
  13. 3、 设置点击状态
  14. 4、设置非选中状态
  15. 5、指示器的改变
  16. 5、点击block
  17. 6、传入数据方法
  18. */
  19. var deviceCount : String?{
  20. didSet{
  21. if let deviceCount = deviceCount {
  22. deviceCountLabel?.text = deviceCount
  23. }
  24. }
  25. }
  26. var deviceType : String?{
  27. didSet{
  28. if let deviceType = deviceType {
  29. deviceTypeLabel?.text = deviceType
  30. }
  31. }
  32. }
  33. //回调方法
  34. var tapCallback:((DeviceTypeBtn)->Void)?
  35. //是否选中 点击状态
  36. open var isSelected:Bool = false{
  37. didSet{
  38. if isSelected {
  39. log.debug("选中\(isSelected)")
  40. deviceTypeLabel?.textColor = UIColor(hexString: "FD8B23")
  41. deviceCountLabel?.textColor = UIColor(hexString: "FD8B23")
  42. indicator?.backgroundColor = UIColor(hexString: "FD8B23")
  43. }else{
  44. log.debug("取消选中\(isSelected)")
  45. deviceTypeLabel?.textColor = UIColor(hexString: "#888888")
  46. deviceCountLabel?.textColor = UIColor(hexString: "#222222")
  47. indicator?.backgroundColor = UIColor.clear
  48. }
  49. }
  50. }
  51. //设备数量
  52. private var deviceCountLabel :UILabel?
  53. //设备类型
  54. private var deviceTypeLabel :UILabel?
  55. //指示器
  56. private var indicator :UIView?
  57. override init(frame: CGRect) {
  58. super.init(frame: frame)
  59. //创建UI
  60. createUI()
  61. //打开手势交互
  62. self.isUserInteractionEnabled = true
  63. //创建手势
  64. let tap = UITapGestureRecognizer(target: self, action: #selector(tapGestrueAction))
  65. addGestureRecognizer(tap)
  66. }
  67. required init?(coder aDecoder: NSCoder) {
  68. fatalError("init(coder:) has not been implemented")
  69. }
  70. func createUI() {
  71. self.backgroundColor = UIColor.white
  72. indicator = UIView()
  73. indicator?.backgroundColor = UIColor.clear
  74. indicator?.layer.masksToBounds = true
  75. indicator?.layer.cornerRadius = 1
  76. addSubview(indicator!)
  77. indicator?.snp.makeConstraints({ (make) in
  78. make.bottom.equalToSuperview()
  79. make.centerX.equalToSuperview()
  80. make.width.equalTo(30)
  81. make.height.equalTo(2)
  82. })
  83. deviceTypeLabel = UILabel()
  84. deviceTypeLabel?.textColor = UIColor(hexString: "888888")
  85. deviceTypeLabel?.textAlignment = .center
  86. deviceTypeLabel?.font = UIFont(name: "PingFangSC-Semibold", size: 10)
  87. deviceTypeLabel?.text = "全部设备"
  88. addSubview(deviceTypeLabel!)
  89. deviceTypeLabel?.snp.makeConstraints({ (make) in
  90. make.bottom.equalTo(indicator?.snp.top ?? 0).offset(-16)
  91. make.centerX.equalToSuperview()
  92. make.height.equalTo(14)
  93. })
  94. deviceCountLabel = UILabel()
  95. deviceCountLabel?.textColor = UIColor(hexString: "222222")
  96. deviceCountLabel?.textAlignment = .center
  97. deviceCountLabel?.font = UIFont(name: "PingFang-SC-Regular", size: 25)
  98. deviceCountLabel?.text = "57"
  99. addSubview(deviceCountLabel!)
  100. deviceCountLabel?.snp.makeConstraints({ (make) in
  101. make.bottom.equalTo(deviceTypeLabel?.snp.top ?? 0).offset(-3)
  102. make.centerX.equalToSuperview()
  103. make.height.equalTo(35)
  104. })
  105. }
  106. //添加手势
  107. @objc func tapGestrueAction() {
  108. self.tapCallback!(self)
  109. }
  110. }
  111. class SBTHomeTableViewHeader: UIView {
  112. /*
  113. 刷新函数 : 重新赋值
  114. 当列表数据删除数据,重新刷新头部数据
  115. 删除的时候 ,全部设备改变 ,对应设备类型也跟着改变(重新赋值)
  116. */
  117. var Counts : [String] = ["0","0","0","0"]
  118. // 回调函数
  119. var selectedCallback:((Int)->Void)?
  120. var deviceCounts : [String]?
  121. override init(frame: CGRect) {
  122. super.init(frame: frame)
  123. createUI()
  124. }
  125. required init?(coder aDecoder: NSCoder) {
  126. fatalError("init(coder:) has not been implemented")
  127. }
  128. //刷新函数 ,重新赋值
  129. func reloadDeviceCount() {
  130. for i in 0..<4 {
  131. let btn = self.viewWithTag(667 + i) as! DeviceTypeBtn
  132. btn.deviceCount = deviceCounts![i]
  133. }
  134. }
  135. func createUI() {
  136. let types = ["全部设备","太阳能控制器","逆变器","逆变一体机"]
  137. let buttonW = (KSCREENWIDTH ) / 4 //宽
  138. let buttonH = 112.0
  139. for i in 0..<4 {
  140. let btnView = DeviceTypeBtn(frame: CGRect(x: (buttonW ) * CGFloat(i), y: 0, width: buttonW, height: CGFloat(buttonH)))
  141. btnView.tag = 667 + i
  142. btnView.deviceType = types[i]
  143. btnView.deviceCount = Counts[i]
  144. addSubview(btnView)
  145. if btnView.tag == 667 {
  146. btnView.isSelected = true
  147. }
  148. btnView.tapCallback = {(btn) in
  149. log.debug("点击了\(btnView.isSelected)")
  150. self.didSelectBtn(sender: btn)
  151. }
  152. }
  153. }
  154. func didSelectBtn(sender:DeviceTypeBtn) {
  155. let btn1 = self.viewWithTag(667) as! DeviceTypeBtn
  156. let btn2 = self.viewWithTag(667 + 1) as! DeviceTypeBtn
  157. let btn3 = self.viewWithTag(667 + 2) as! DeviceTypeBtn
  158. let btn4 = self.viewWithTag(667 + 3) as! DeviceTypeBtn
  159. btn1.isSelected = false
  160. btn2.isSelected = false
  161. btn3.isSelected = false
  162. btn4.isSelected = false
  163. sender.isSelected = true
  164. self.selectedCallback!(sender.tag - 667)
  165. }
  166. }