IHLampControlView.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. //
  2. // IHLampControlView.swift
  3. // Inhealth
  4. //
  5. // Created by weclouds on 2020/4/17.
  6. // Copyright © 2020 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. import PKHUD
  10. protocol IHLampControlViewDelegate : NSObjectProtocol{
  11. func regulateLampControlSwitch(_ status: String)
  12. func regulateLampControlBrightness(_ light: String)
  13. func regulateLampControlColorTemperature(_ color: String)
  14. //学校的 - 亮度
  15. func schoolRegulateLampControlBrightness(_ light: String)
  16. //学校的 - 色温
  17. func schoolRegulateLampControlColorTemperature(_ color: String)
  18. //学校的单个灯 - 开关
  19. func schoolRegulateLampControlSwitch(_ status: String)
  20. func regulateLampControlColor(_ red: String, green: String, blue: String)
  21. }
  22. class IHLampControlView: UIView {
  23. //加---
  24. var isClassRoom :Bool? {
  25. didSet{
  26. if let isClassRoom = isClassRoom{
  27. if isClassRoom == true {
  28. self.colorBtn.isHidden = true
  29. self.whiteBtn.isHidden = true
  30. let x = (KSCREENWIDTH - 50) * 0.5
  31. self.closeBtn.frame = CGRect(x: x, y: 0, width: 50, height: 75)
  32. }
  33. }
  34. }
  35. }
  36. var schoolLight : ShoolDeviceList?{
  37. didSet{
  38. guard let schoolLight = schoolLight else {
  39. return
  40. }
  41. if let colorTemp = schoolLight.color,let bright = schoolLight.light ,let status = schoolLight.status{
  42. whiteView.lightMode = "5"
  43. whiteView.model = schoolLight.model
  44. whiteView.currentBrightness = "\(bright)"
  45. whiteView.currentColorTemp = "\(colorTemp)"
  46. colorView.currentColor = ColorStruct(Red: 0 , Green: 0, Blue: 0)
  47. if status == "0" {
  48. closeBtn.isSelected = false
  49. closeView.isHidden = false
  50. colorView.isHidden = true
  51. whiteView.isHidden = true
  52. whiteBtn.isSelected = false
  53. colorBtn.isSelected = false
  54. }else if status == "1"{
  55. if Intermediate.isRegulateColor == "0" {
  56. closeBtn.isSelected = true
  57. closeView.isHidden = true
  58. colorView.isHidden = true
  59. whiteView.isHidden = false
  60. whiteBtn.isSelected = true
  61. colorBtn.isSelected = false
  62. }else if Intermediate.isRegulateColor == "1"{
  63. closeBtn.isSelected = true
  64. closeView.isHidden = true
  65. colorView.isHidden = false
  66. whiteView.isHidden = true
  67. whiteBtn.isSelected = false
  68. colorBtn.isSelected = true
  69. }
  70. }
  71. }
  72. }
  73. }
  74. weak var delegate : IHLampControlViewDelegate?
  75. var lightData : LightInfodata?{
  76. didSet{
  77. if let lightData = self.lightData {
  78. if let model = lightData.model ,let colorTemp = lightData.color,let r = lightData.colorR,let g = lightData.colorG,let b = lightData.colorB,let bright = lightData.light ,let status = lightData.status{
  79. setButtonFrame(model)
  80. whiteView.lightMode = model
  81. whiteView.currentBrightness = "\(bright)"
  82. whiteView.currentColorTemp = "\(colorTemp)"
  83. colorView.currentColor = ColorStruct(Red: r , Green: g, Blue: b)
  84. if status == "0" {
  85. closeBtn.isSelected = false
  86. closeView.isHidden = false
  87. colorView.isHidden = true
  88. whiteView.isHidden = true
  89. whiteBtn.isSelected = false
  90. colorBtn.isSelected = false
  91. }else if status == "1"{
  92. if Intermediate.isRegulateColor == "0" {
  93. closeBtn.isSelected = true
  94. closeView.isHidden = true
  95. colorView.isHidden = true
  96. whiteView.isHidden = false
  97. whiteBtn.isSelected = true
  98. colorBtn.isSelected = false
  99. }else if Intermediate.isRegulateColor == "1"{
  100. closeBtn.isSelected = true
  101. closeView.isHidden = true
  102. colorView.isHidden = false
  103. whiteView.isHidden = true
  104. whiteBtn.isSelected = false
  105. colorBtn.isSelected = true
  106. }
  107. }
  108. }
  109. }
  110. }
  111. }
  112. private let gradientLayer = IHRadialGradientLayer()
  113. @IBOutlet weak var buttonBar: UIView!
  114. @IBOutlet weak var whiteBtn: UIButton!
  115. @IBOutlet weak var closeBtn: UIButton!
  116. @IBOutlet weak var colorBtn: UIButton!
  117. lazy var closeView: IHLampCloseView = {
  118. let closeView = Bundle.main.loadNibNamed("IHLampCloseView", owner: nil, options: nil)?.first as! IHLampCloseView
  119. closeView.isHidden = true
  120. return closeView
  121. }()
  122. lazy var whiteView: IHWhiltLightView = {
  123. let whiteView = Bundle.main.loadNibNamed("IHWhiltLightView", owner: nil, options: nil)?.first as! IHWhiltLightView
  124. whiteView.isHidden = true
  125. return whiteView
  126. }()
  127. lazy var colorView: IHLampColorView = {
  128. let colorView = Bundle.main.loadNibNamed("IHLampColorView", owner: nil, options: nil)?.first as! IHLampColorView
  129. colorView.isHidden = true
  130. return colorView
  131. }()
  132. override func awakeFromNib() {
  133. super.awakeFromNib()
  134. setupUI()
  135. }
  136. override func layoutSubviews() {
  137. super.layoutSubviews()
  138. self.frame = CGRect(x: 0, y: 0, width: KSCREENWIDTH, height: KSCREENHEIGHT)
  139. closeView.frame = CGRect(x: 0, y: 0, width: KSCREENWIDTH, height: KSCREENHEIGHT)
  140. whiteView.frame = CGRect(x: 0, y: 0, width: KSCREENWIDTH, height: KSCREENHEIGHT)
  141. colorView.frame = CGRect(x: 0, y: 0, width: KSCREENWIDTH, height: KSCREENHEIGHT)
  142. }
  143. func setupUI() {
  144. whiteBtn.layoutButton(style: .Top, imageTitleSpace: 10)
  145. closeBtn.layoutButton(style: .Top, imageTitleSpace: 10)
  146. colorBtn.layoutButton(style: .Top, imageTitleSpace: 10)
  147. insertSubview(closeView, at: 0)
  148. insertSubview(whiteView, at: 0)
  149. insertSubview(colorView, at: 0)
  150. callbackAction()
  151. }
  152. @IBAction func closeAction(_ sender: UIButton) {
  153. sender.isSelected = !sender.isSelected
  154. //这里不需要立即处理 刷新之后才处理
  155. if sender.isSelected == true {
  156. //closeView.isHidden = true
  157. // colorView.isHidden = true
  158. // whiteView.isHidden = false
  159. // whiteBtn.isSelected = true
  160. colorBtn.isSelected = false
  161. }else{
  162. // closeView.isHidden = false
  163. // colorView.isHidden = true
  164. // whiteView.isHidden = true
  165. // whiteBtn.isSelected = false
  166. // colorBtn.isSelected = false
  167. }
  168. if let delegate = self.delegate {
  169. if isClassRoom == true {
  170. delegate.schoolRegulateLampControlSwitch(sender.isSelected == true ? "1" : "0")
  171. }else{
  172. delegate.regulateLampControlSwitch(sender.isSelected == true ? "1" : "0")
  173. }
  174. }
  175. }
  176. @IBAction func whiteAction(_ sender: UIButton) {
  177. if closeBtn.isSelected == false {
  178. g_showHUD("设备已离线")
  179. return
  180. }
  181. //sender.isSelected = !sender.isSelected
  182. closeView.isHidden = true
  183. colorView.isHidden = true
  184. whiteView.isHidden = false
  185. whiteBtn.isSelected = true
  186. colorBtn.isSelected = false
  187. closeBtn.isSelected = true
  188. }
  189. @IBAction func colorAction(_ sender: UIButton) {
  190. // sender.isSelected = !sender.isSelected
  191. if closeBtn.isSelected == false {
  192. g_showHUD("设备已离线")
  193. return
  194. }
  195. closeView.isHidden = true
  196. colorView.isHidden = false
  197. whiteView.isHidden = true
  198. whiteBtn.isSelected = false
  199. colorBtn.isSelected = true
  200. closeBtn.isSelected = true
  201. }
  202. func callbackAction() {
  203. whiteView.lampControlTrunoff = {
  204. // self.closeView.isHidden = false
  205. // self.colorView.isHidden = true
  206. // self.whiteView.isHidden = true
  207. // self.whiteBtn.isSelected = false
  208. // self.colorBtn.isSelected = false
  209. }
  210. whiteView.brightnessCallback = { (bright) in
  211. if let delegate = self.delegate {
  212. if self.isClassRoom == true {
  213. //学校的
  214. delegate.schoolRegulateLampControlBrightness(bright)
  215. }else{
  216. //酒店的
  217. delegate.regulateLampControlBrightness(bright)
  218. }
  219. }
  220. }
  221. whiteView.colorTempCallback = {[unowned self] (color) in
  222. if let delegate = self.delegate {
  223. if self.isClassRoom == true{
  224. //学校的
  225. delegate.schoolRegulateLampControlColorTemperature(color)
  226. }else{
  227. //酒店的
  228. if self.colorBtn.isHidden == false{
  229. delegate.regulateLampControlColorTemperature(color)
  230. }
  231. }
  232. }
  233. }
  234. colorView.paletteComplition = {[unowned self] (rgb) in
  235. if let delegate = self.delegate {
  236. delegate.regulateLampControlColor("\(rgb.Red!)", green: "\(rgb.Green!)", blue: "\(rgb.Blue!)")
  237. }
  238. }
  239. }
  240. }
  241. extension IHLampControlView {
  242. func setButtonFrame(_ model:String) {
  243. if model == "0" || model == "1" || model == "4" || model == "5" {
  244. self.colorBtn.isHidden = true
  245. let beginX = (KSCREENWIDTH - 50 * 2 - 90) / 2
  246. self.closeBtn.frame = CGRect(x: beginX, y: 0, width: 50, height: 75)
  247. self.whiteBtn.frame = CGRect(x: beginX + 50 + 90, y: 0, width: 50, height: 75)
  248. }else {
  249. self.colorBtn.isHidden = false
  250. let beginX = (KSCREENWIDTH - 50 * 3 - 50 * 2) / 2
  251. self.closeBtn.frame = CGRect(x: beginX, y: 0, width: 50, height: 75)
  252. self.whiteBtn.frame = CGRect(x: beginX + 50 * 2, y: 0, width: 50, height: 75)
  253. self.colorBtn.frame = CGRect(x: beginX + 50 * 4, y: 0, width: 50, height: 75)
  254. }
  255. }
  256. }