// // IHLampControlView.swift // Inhealth // // Created by weclouds on 2020/4/17. // Copyright © 2020 weclouds. All rights reserved. // import UIKit import PKHUD protocol IHLampControlViewDelegate : NSObjectProtocol{ func regulateLampControlSwitch(_ status: String) func regulateLampControlBrightness(_ light: String) func regulateLampControlColorTemperature(_ color: String) //学校的 - 亮度 func schoolRegulateLampControlBrightness(_ light: String) //学校的 - 色温 func schoolRegulateLampControlColorTemperature(_ color: String) //学校的单个灯 - 开关 func schoolRegulateLampControlSwitch(_ status: String) func regulateLampControlColor(_ red: String, green: String, blue: String) } class IHLampControlView: UIView { //加--- var isClassRoom :Bool? { didSet{ if let isClassRoom = isClassRoom{ if isClassRoom == true { self.colorBtn.isHidden = true self.whiteBtn.isHidden = true let x = (KSCREENWIDTH - 50) * 0.5 self.closeBtn.frame = CGRect(x: x, y: 0, width: 50, height: 75) } } } } var schoolLight : ShoolDeviceList?{ didSet{ guard let schoolLight = schoolLight else { return } if let colorTemp = schoolLight.color,let bright = schoolLight.light ,let status = schoolLight.status{ whiteView.lightMode = "5" whiteView.model = schoolLight.model whiteView.currentBrightness = "\(bright)" whiteView.currentColorTemp = "\(colorTemp)" colorView.currentColor = ColorStruct(Red: 0 , Green: 0, Blue: 0) if status == "0" { closeBtn.isSelected = false closeView.isHidden = false colorView.isHidden = true whiteView.isHidden = true whiteBtn.isSelected = false colorBtn.isSelected = false }else if status == "1"{ if Intermediate.isRegulateColor == "0" { closeBtn.isSelected = true closeView.isHidden = true colorView.isHidden = true whiteView.isHidden = false whiteBtn.isSelected = true colorBtn.isSelected = false }else if Intermediate.isRegulateColor == "1"{ closeBtn.isSelected = true closeView.isHidden = true colorView.isHidden = false whiteView.isHidden = true whiteBtn.isSelected = false colorBtn.isSelected = true } } } } } weak var delegate : IHLampControlViewDelegate? var lightData : LightInfodata?{ didSet{ if let lightData = self.lightData { 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{ setButtonFrame(model) whiteView.lightMode = model whiteView.currentBrightness = "\(bright)" whiteView.currentColorTemp = "\(colorTemp)" colorView.currentColor = ColorStruct(Red: r , Green: g, Blue: b) if status == "0" { closeBtn.isSelected = false closeView.isHidden = false colorView.isHidden = true whiteView.isHidden = true whiteBtn.isSelected = false colorBtn.isSelected = false }else if status == "1"{ if Intermediate.isRegulateColor == "0" { closeBtn.isSelected = true closeView.isHidden = true colorView.isHidden = true whiteView.isHidden = false whiteBtn.isSelected = true colorBtn.isSelected = false }else if Intermediate.isRegulateColor == "1"{ closeBtn.isSelected = true closeView.isHidden = true colorView.isHidden = false whiteView.isHidden = true whiteBtn.isSelected = false colorBtn.isSelected = true } } } } } } private let gradientLayer = IHRadialGradientLayer() @IBOutlet weak var buttonBar: UIView! @IBOutlet weak var whiteBtn: UIButton! @IBOutlet weak var closeBtn: UIButton! @IBOutlet weak var colorBtn: UIButton! lazy var closeView: IHLampCloseView = { let closeView = Bundle.main.loadNibNamed("IHLampCloseView", owner: nil, options: nil)?.first as! IHLampCloseView closeView.isHidden = true return closeView }() lazy var whiteView: IHWhiltLightView = { let whiteView = Bundle.main.loadNibNamed("IHWhiltLightView", owner: nil, options: nil)?.first as! IHWhiltLightView whiteView.isHidden = true return whiteView }() lazy var colorView: IHLampColorView = { let colorView = Bundle.main.loadNibNamed("IHLampColorView", owner: nil, options: nil)?.first as! IHLampColorView colorView.isHidden = true return colorView }() override func awakeFromNib() { super.awakeFromNib() setupUI() } override func layoutSubviews() { super.layoutSubviews() self.frame = CGRect(x: 0, y: 0, width: KSCREENWIDTH, height: KSCREENHEIGHT) closeView.frame = CGRect(x: 0, y: 0, width: KSCREENWIDTH, height: KSCREENHEIGHT) whiteView.frame = CGRect(x: 0, y: 0, width: KSCREENWIDTH, height: KSCREENHEIGHT) colorView.frame = CGRect(x: 0, y: 0, width: KSCREENWIDTH, height: KSCREENHEIGHT) } func setupUI() { whiteBtn.layoutButton(style: .Top, imageTitleSpace: 10) closeBtn.layoutButton(style: .Top, imageTitleSpace: 10) colorBtn.layoutButton(style: .Top, imageTitleSpace: 10) insertSubview(closeView, at: 0) insertSubview(whiteView, at: 0) insertSubview(colorView, at: 0) callbackAction() } @IBAction func closeAction(_ sender: UIButton) { sender.isSelected = !sender.isSelected //这里不需要立即处理 刷新之后才处理 if sender.isSelected == true { //closeView.isHidden = true // colorView.isHidden = true // whiteView.isHidden = false // whiteBtn.isSelected = true colorBtn.isSelected = false }else{ // closeView.isHidden = false // colorView.isHidden = true // whiteView.isHidden = true // whiteBtn.isSelected = false // colorBtn.isSelected = false } if let delegate = self.delegate { if isClassRoom == true { delegate.schoolRegulateLampControlSwitch(sender.isSelected == true ? "1" : "0") }else{ delegate.regulateLampControlSwitch(sender.isSelected == true ? "1" : "0") } } } @IBAction func whiteAction(_ sender: UIButton) { if closeBtn.isSelected == false { g_showHUD("设备已离线") return } //sender.isSelected = !sender.isSelected closeView.isHidden = true colorView.isHidden = true whiteView.isHidden = false whiteBtn.isSelected = true colorBtn.isSelected = false closeBtn.isSelected = true } @IBAction func colorAction(_ sender: UIButton) { // sender.isSelected = !sender.isSelected if closeBtn.isSelected == false { g_showHUD("设备已离线") return } closeView.isHidden = true colorView.isHidden = false whiteView.isHidden = true whiteBtn.isSelected = false colorBtn.isSelected = true closeBtn.isSelected = true } func callbackAction() { whiteView.lampControlTrunoff = { // self.closeView.isHidden = false // self.colorView.isHidden = true // self.whiteView.isHidden = true // self.whiteBtn.isSelected = false // self.colorBtn.isSelected = false } whiteView.brightnessCallback = { (bright) in if let delegate = self.delegate { if self.isClassRoom == true { //学校的 delegate.schoolRegulateLampControlBrightness(bright) }else{ //酒店的 delegate.regulateLampControlBrightness(bright) } } } whiteView.colorTempCallback = {[unowned self] (color) in if let delegate = self.delegate { if self.isClassRoom == true{ //学校的 delegate.schoolRegulateLampControlColorTemperature(color) }else{ //酒店的 if self.colorBtn.isHidden == false{ delegate.regulateLampControlColorTemperature(color) } } } } colorView.paletteComplition = {[unowned self] (rgb) in if let delegate = self.delegate { delegate.regulateLampControlColor("\(rgb.Red!)", green: "\(rgb.Green!)", blue: "\(rgb.Blue!)") } } } } extension IHLampControlView { func setButtonFrame(_ model:String) { if model == "0" || model == "1" || model == "4" || model == "5" { self.colorBtn.isHidden = true let beginX = (KSCREENWIDTH - 50 * 2 - 90) / 2 self.closeBtn.frame = CGRect(x: beginX, y: 0, width: 50, height: 75) self.whiteBtn.frame = CGRect(x: beginX + 50 + 90, y: 0, width: 50, height: 75) }else { self.colorBtn.isHidden = false let beginX = (KSCREENWIDTH - 50 * 3 - 50 * 2) / 2 self.closeBtn.frame = CGRect(x: beginX, y: 0, width: 50, height: 75) self.whiteBtn.frame = CGRect(x: beginX + 50 * 2, y: 0, width: 50, height: 75) self.colorBtn.frame = CGRect(x: beginX + 50 * 4, y: 0, width: 50, height: 75) } } }