IHClassLightSettingCell.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. //
  2. // IHClassLightSettingCell.swift
  3. // Inhealth
  4. //
  5. // Created by weclouds on 2020/7/24.
  6. // Copyright © 2020 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. let KNOtificationClassLightReloadData = "KNOtificationClassLightReloadData"
  10. class IHClassLightSettingCell: UITableViewCell {
  11. private var labelTitle : UILabel? //上
  12. private var leftBrightnessBtn : UIButton?
  13. private var tableView : UITableView?
  14. var indexPath : IndexPath?
  15. private var valueBackView : UIView?
  16. private var choseValue : String?
  17. private var rightColorBtn : UIButton?
  18. private var choseBtnTag : Int = 0
  19. let brightnessValues = ["自动","100","90","80","70","60","50","40","30","20","10","0"]
  20. let colorValues = ["3000","3300","3600","3900","4200","4500","4800","5100","5400","5700","6000"]
  21. var dataSource : [String : [String : String]]? {
  22. didSet{
  23. labelTitle?.text = dataSource?.keys.first!
  24. let dic = dataSource?[(labelTitle?.text)!]
  25. if let brightness = dic?["Brightness"] {
  26. if Int(brightness)! > 100{
  27. self.leftBrightnessBtn?.setTitle("自动", for: .normal)
  28. }else{
  29. self.leftBrightnessBtn?.setTitle("\(brightness)%", for: .normal)
  30. }
  31. }
  32. if let color = dic?["Color temperature"] {
  33. self.rightColorBtn?.setTitle("\(color)k", for: .normal)
  34. }
  35. }
  36. }
  37. class func classLightWithTableView(tableView: UITableView , indexPath : IndexPath) ->IHClassLightSettingCell {
  38. let cellId = "classLights"
  39. var cell = tableView.dequeueReusableCell(withIdentifier: cellId)
  40. if cell == nil {
  41. cell = IHClassLightSettingCell.init(style: .default, reuseIdentifier: cellId,tableView : tableView, indexPath : indexPath)
  42. }
  43. return cell as! IHClassLightSettingCell
  44. }
  45. init(style: UITableViewCell.CellStyle, reuseIdentifier: String?,tableView : UITableView, indexPath : IndexPath) {
  46. super.init(style: style, reuseIdentifier: reuseIdentifier)
  47. // self.indexPath = indexPath
  48. self.tableView = tableView
  49. createUI()
  50. }
  51. // override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  52. // super.init(style: style, reuseIdentifier: reuseIdentifier)
  53. //
  54. // createUI()
  55. // }
  56. required init?(coder: NSCoder) {
  57. fatalError("init(coder:) has not been implemented")
  58. }
  59. func createUI() {
  60. let backView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: KSCREENWIDTH, height: 120))
  61. backView.backgroundColor = .white
  62. //line
  63. let line = UIView.init(frame: CGRect.init(x: 0, y: 119, width: KSCREENWIDTH, height: 1))
  64. line.backgroundColor = UIColor.init(red: 246/255.0, green: 245/255.0, blue: 248/255.0, alpha: 1.0)
  65. backView.addSubview(line)
  66. //上
  67. let labelTitle = UILabel.init(frame: CGRect.init(x: 5, y: 0, width: KSCREENWIDTH, height: 40))
  68. labelTitle.backgroundColor = UIColor.init(red: 246/255.0, green: 245/255.0, blue: 248/255.0, alpha: 1.0)
  69. backView.addSubview(labelTitle)
  70. self.labelTitle = labelTitle
  71. let h : CGFloat = 50
  72. //下
  73. let choseView = UIView.init(frame: CGRect.init(x: 0, y: 40, width: KSCREENWIDTH, height: h))
  74. backView.addSubview(choseView)
  75. //左
  76. let leftView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: KSCREENWIDTH * 0.5 - 14, height: h))
  77. let leftLabel = UILabel.init(frame: CGRect.init(x: 0, y: 0, width: (KSCREENWIDTH * 0.5 - 14) * 0.5, height: h))
  78. leftLabel.text = "亮度"
  79. leftLabel.font = UIFont.systemFont(ofSize: 15)
  80. leftLabel.textAlignment = .right
  81. leftView.addSubview(leftLabel)
  82. let leftBrightnessBtn = UIButton.init(frame: CGRect.init(x: (KSCREENWIDTH * 0.5 - 14) * 0.5 + 5, y: 0, width: (KSCREENWIDTH * 0.5 - 14) * 0.5 - 9, height: h))
  83. leftBrightnessBtn.setTitle("100", for: .normal)
  84. leftBrightnessBtn.setTitleColor(.black, for: .normal)
  85. leftBrightnessBtn.setImage(UIImage.init(named: "xialaIcon"), for: .normal)
  86. leftBrightnessBtn.titleLabel?.font = UIFont.systemFont(ofSize: 15.0)
  87. leftBrightnessBtn.titleEdgeInsets = UIEdgeInsets.init(top: 0, left: -10, bottom: 0, right: 0)
  88. leftBrightnessBtn.semanticContentAttribute = .forceRightToLeft
  89. leftBrightnessBtn.addTarget(self, action: #selector(choseBrightness), for: .touchUpInside)
  90. leftBrightnessBtn.tag = 1
  91. leftView.addSubview(leftBrightnessBtn)
  92. choseView.addSubview(leftView)
  93. self.leftBrightnessBtn = leftBrightnessBtn
  94. //中间线
  95. // let x = KSCREENWIDTH * 0.5 - leftBrightnessBtn.frame.origin.x - leftBrightnessBtn.width - 2
  96. // let betweenLine = UIView.init(frame: CGRect.init(x: x, y: 0, width: 1, height: h))
  97. // betweenLine.backgroundColor = UIColor.init(red: 246/255.0, green: 245/255.0, blue: 248/255.0, alpha: 1.0)
  98. // leftView.addSubview(betweenLine)
  99. //
  100. //右
  101. let rightView = UIView.init(frame: CGRect.init(x: KSCREENWIDTH * 0.5 - 15, y: 0, width: KSCREENWIDTH * 0.5 + 15, height: h))
  102. let rightLabel = UILabel.init(frame: CGRect.init(x: 0, y: 0, width: (KSCREENWIDTH * 0.5 + 15) * 0.5 + 25, height: h))
  103. rightLabel.text = "色温"
  104. rightLabel.font = UIFont.systemFont(ofSize: 15.0)
  105. rightLabel.textAlignment = .right
  106. rightView.addSubview(rightLabel)
  107. let rightColorBtn = UIButton.init(frame: CGRect.init(x: (KSCREENWIDTH * 0.5 + 15) * 0.5 + 17, y: 0, width: (KSCREENWIDTH * 0.5 + 15) * 0.5 - 25, height: h))
  108. rightColorBtn.setTitle("4500k", for: .normal)
  109. rightColorBtn.setTitleColor(.black, for: .normal)
  110. rightColorBtn.setImage(UIImage.init(named: "xialaIcon"), for: .normal)
  111. rightColorBtn.titleLabel?.font = UIFont.systemFont(ofSize: 15.0)
  112. rightColorBtn.imageEdgeInsets = UIEdgeInsets.init(top: 0, left: 10, bottom: 0, right: 0)
  113. rightColorBtn.semanticContentAttribute = .forceRightToLeft
  114. rightColorBtn.addTarget(self, action: #selector(choseColor), for: .touchUpInside)
  115. rightColorBtn.tag = 2
  116. rightView.addSubview(rightColorBtn)
  117. choseView.addSubview(rightView)
  118. self.rightColorBtn = rightColorBtn
  119. self.contentView.addSubview(backView)
  120. }
  121. }
  122. extension IHClassLightSettingCell {
  123. @objc func choseBrightness(sender : UIButton){
  124. //选择: 亮度
  125. log.debug("亮度:\(self.indexPath)")
  126. self.choseBtnTag = sender.tag
  127. if valueBackView != nil {
  128. valueBackView?.removeFromSuperview()
  129. }
  130. createChosePicker(sender: sender)
  131. }
  132. @objc func choseColor (sender : UIButton){
  133. //选择: 色温
  134. log.debug("色温:\(self.indexPath)")
  135. self.choseBtnTag = sender.tag
  136. if valueBackView != nil {
  137. valueBackView?.removeFromSuperview()
  138. }
  139. createChosePicker(sender: sender)
  140. }
  141. }
  142. extension IHClassLightSettingCell{
  143. func createChosePicker(sender : UIButton) {
  144. let point = sender.convert(sender.frame.origin, to: self.tableView)
  145. var valueBackView = UIView.init(frame: CGRect.init(x: point.x - 90, y: point.y + 45, width: 90, height: 150))
  146. if self.choseBtnTag == 2 {
  147. valueBackView.frame.origin.x = valueBackView.frame.origin.x - 35
  148. }
  149. //屏幕底下的显示
  150. if (KSCREENHEIGHT - point.y) < 150 {
  151. //左侧的
  152. valueBackView = UIView.init(frame: CGRect.init(x: point.x - 160, y: point.y - 150, width: 90, height: 150))
  153. }
  154. valueBackView.backgroundColor = .lightGray
  155. self.tableView?.addSubview(valueBackView)
  156. self.valueBackView = valueBackView
  157. let valueTableView = UITableView.init(frame: CGRect.init(x: 1, y: 1, width: 88, height: 148))
  158. valueTableView.dataSource = self
  159. valueTableView.delegate = self
  160. valueBackView.addSubview(valueTableView)
  161. }
  162. }
  163. extension IHClassLightSettingCell: UITableViewDelegate,UITableViewDataSource{
  164. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  165. if self.choseBtnTag == 1 {
  166. return self.brightnessValues.count
  167. }else{
  168. return self.colorValues.count
  169. }
  170. }
  171. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  172. let valueId = "valueId"
  173. var cell = tableView.dequeueReusableCell(withIdentifier: valueId)
  174. if cell == nil {
  175. cell = UITableViewCell.init(style: .default, reuseIdentifier: valueId)
  176. }
  177. if self.choseBtnTag == 1{
  178. let brightness = self.brightnessValues[indexPath.row]
  179. if brightness == "自动"{
  180. cell?.textLabel?.text = "自动"
  181. }else{
  182. cell?.textLabel?.text = "\(brightness)%"
  183. }
  184. }else{
  185. cell?.textLabel?.text = "\(self.colorValues[indexPath.row])k"
  186. }
  187. cell?.textLabel?.textAlignment = .center
  188. return cell!
  189. }
  190. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  191. //choseValue = self.dataSource?.values.first!
  192. if self.choseBtnTag == 1 {
  193. choseValue = self.brightnessValues[indexPath.row]
  194. if choseValue == "自动"{
  195. leftBrightnessBtn?.setTitle("自动", for: .normal)
  196. }else{
  197. leftBrightnessBtn?.setTitle("\(choseValue!)%", for: .normal)
  198. }
  199. }else{
  200. choseValue = self.colorValues[indexPath.row]
  201. rightColorBtn?.setTitle("\(choseValue!)k", for: .normal)
  202. }
  203. log.debug("value: \(String(describing: choseValue))")
  204. //更新数据
  205. NotificationCenter.default.post(name: NSNotification.Name(rawValue: KNOtificationClassLightReloadData), object: nil, userInfo: ["indexPath":self.indexPath!,"choseBtnTag": choseBtnTag,"choseValue" : choseValue!])
  206. self.choseBtnTag = 0
  207. self.valueBackView?.removeFromSuperview()
  208. }
  209. }