// // IHClassLightSettingCell.swift // Inhealth // // Created by weclouds on 2020/7/24. // Copyright © 2020 weclouds. All rights reserved. // import UIKit let KNOtificationClassLightReloadData = "KNOtificationClassLightReloadData" class IHClassLightSettingCell: UITableViewCell { private var labelTitle : UILabel? //上 private var leftBrightnessBtn : UIButton? private var tableView : UITableView? var indexPath : IndexPath? private var valueBackView : UIView? private var choseValue : String? private var rightColorBtn : UIButton? private var choseBtnTag : Int = 0 let brightnessValues = ["自动","100","90","80","70","60","50","40","30","20","10","0"] let colorValues = ["3000","3300","3600","3900","4200","4500","4800","5100","5400","5700","6000"] var dataSource : [String : [String : String]]? { didSet{ labelTitle?.text = dataSource?.keys.first! let dic = dataSource?[(labelTitle?.text)!] if let brightness = dic?["Brightness"] { if Int(brightness)! > 100{ self.leftBrightnessBtn?.setTitle("自动", for: .normal) }else{ self.leftBrightnessBtn?.setTitle("\(brightness)%", for: .normal) } } if let color = dic?["Color temperature"] { self.rightColorBtn?.setTitle("\(color)k", for: .normal) } } } class func classLightWithTableView(tableView: UITableView , indexPath : IndexPath) ->IHClassLightSettingCell { let cellId = "classLights" var cell = tableView.dequeueReusableCell(withIdentifier: cellId) if cell == nil { cell = IHClassLightSettingCell.init(style: .default, reuseIdentifier: cellId,tableView : tableView, indexPath : indexPath) } return cell as! IHClassLightSettingCell } init(style: UITableViewCell.CellStyle, reuseIdentifier: String?,tableView : UITableView, indexPath : IndexPath) { super.init(style: style, reuseIdentifier: reuseIdentifier) // self.indexPath = indexPath self.tableView = tableView createUI() } // override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { // super.init(style: style, reuseIdentifier: reuseIdentifier) // // createUI() // } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } func createUI() { let backView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: KSCREENWIDTH, height: 120)) backView.backgroundColor = .white //line let line = UIView.init(frame: CGRect.init(x: 0, y: 119, width: KSCREENWIDTH, height: 1)) line.backgroundColor = UIColor.init(red: 246/255.0, green: 245/255.0, blue: 248/255.0, alpha: 1.0) backView.addSubview(line) //上 let labelTitle = UILabel.init(frame: CGRect.init(x: 5, y: 0, width: KSCREENWIDTH, height: 40)) labelTitle.backgroundColor = UIColor.init(red: 246/255.0, green: 245/255.0, blue: 248/255.0, alpha: 1.0) backView.addSubview(labelTitle) self.labelTitle = labelTitle let h : CGFloat = 50 //下 let choseView = UIView.init(frame: CGRect.init(x: 0, y: 40, width: KSCREENWIDTH, height: h)) backView.addSubview(choseView) //左 let leftView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: KSCREENWIDTH * 0.5 - 14, height: h)) let leftLabel = UILabel.init(frame: CGRect.init(x: 0, y: 0, width: (KSCREENWIDTH * 0.5 - 14) * 0.5, height: h)) leftLabel.text = "亮度" leftLabel.font = UIFont.systemFont(ofSize: 15) leftLabel.textAlignment = .right leftView.addSubview(leftLabel) 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)) leftBrightnessBtn.setTitle("100", for: .normal) leftBrightnessBtn.setTitleColor(.black, for: .normal) leftBrightnessBtn.setImage(UIImage.init(named: "xialaIcon"), for: .normal) leftBrightnessBtn.titleLabel?.font = UIFont.systemFont(ofSize: 15.0) leftBrightnessBtn.titleEdgeInsets = UIEdgeInsets.init(top: 0, left: -10, bottom: 0, right: 0) leftBrightnessBtn.semanticContentAttribute = .forceRightToLeft leftBrightnessBtn.addTarget(self, action: #selector(choseBrightness), for: .touchUpInside) leftBrightnessBtn.tag = 1 leftView.addSubview(leftBrightnessBtn) choseView.addSubview(leftView) self.leftBrightnessBtn = leftBrightnessBtn //中间线 // let x = KSCREENWIDTH * 0.5 - leftBrightnessBtn.frame.origin.x - leftBrightnessBtn.width - 2 // let betweenLine = UIView.init(frame: CGRect.init(x: x, y: 0, width: 1, height: h)) // betweenLine.backgroundColor = UIColor.init(red: 246/255.0, green: 245/255.0, blue: 248/255.0, alpha: 1.0) // leftView.addSubview(betweenLine) // //右 let rightView = UIView.init(frame: CGRect.init(x: KSCREENWIDTH * 0.5 - 15, y: 0, width: KSCREENWIDTH * 0.5 + 15, height: h)) let rightLabel = UILabel.init(frame: CGRect.init(x: 0, y: 0, width: (KSCREENWIDTH * 0.5 + 15) * 0.5 + 25, height: h)) rightLabel.text = "色温" rightLabel.font = UIFont.systemFont(ofSize: 15.0) rightLabel.textAlignment = .right rightView.addSubview(rightLabel) 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)) rightColorBtn.setTitle("4500k", for: .normal) rightColorBtn.setTitleColor(.black, for: .normal) rightColorBtn.setImage(UIImage.init(named: "xialaIcon"), for: .normal) rightColorBtn.titleLabel?.font = UIFont.systemFont(ofSize: 15.0) rightColorBtn.imageEdgeInsets = UIEdgeInsets.init(top: 0, left: 10, bottom: 0, right: 0) rightColorBtn.semanticContentAttribute = .forceRightToLeft rightColorBtn.addTarget(self, action: #selector(choseColor), for: .touchUpInside) rightColorBtn.tag = 2 rightView.addSubview(rightColorBtn) choseView.addSubview(rightView) self.rightColorBtn = rightColorBtn self.contentView.addSubview(backView) } } extension IHClassLightSettingCell { @objc func choseBrightness(sender : UIButton){ //选择: 亮度 log.debug("亮度:\(self.indexPath)") self.choseBtnTag = sender.tag if valueBackView != nil { valueBackView?.removeFromSuperview() } createChosePicker(sender: sender) } @objc func choseColor (sender : UIButton){ //选择: 色温 log.debug("色温:\(self.indexPath)") self.choseBtnTag = sender.tag if valueBackView != nil { valueBackView?.removeFromSuperview() } createChosePicker(sender: sender) } } extension IHClassLightSettingCell{ func createChosePicker(sender : UIButton) { let point = sender.convert(sender.frame.origin, to: self.tableView) var valueBackView = UIView.init(frame: CGRect.init(x: point.x - 90, y: point.y + 45, width: 90, height: 150)) if self.choseBtnTag == 2 { valueBackView.frame.origin.x = valueBackView.frame.origin.x - 35 } //屏幕底下的显示 if (KSCREENHEIGHT - point.y) < 150 { //左侧的 valueBackView = UIView.init(frame: CGRect.init(x: point.x - 160, y: point.y - 150, width: 90, height: 150)) } valueBackView.backgroundColor = .lightGray self.tableView?.addSubview(valueBackView) self.valueBackView = valueBackView let valueTableView = UITableView.init(frame: CGRect.init(x: 1, y: 1, width: 88, height: 148)) valueTableView.dataSource = self valueTableView.delegate = self valueBackView.addSubview(valueTableView) } } extension IHClassLightSettingCell: UITableViewDelegate,UITableViewDataSource{ func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if self.choseBtnTag == 1 { return self.brightnessValues.count }else{ return self.colorValues.count } } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let valueId = "valueId" var cell = tableView.dequeueReusableCell(withIdentifier: valueId) if cell == nil { cell = UITableViewCell.init(style: .default, reuseIdentifier: valueId) } if self.choseBtnTag == 1{ let brightness = self.brightnessValues[indexPath.row] if brightness == "自动"{ cell?.textLabel?.text = "自动" }else{ cell?.textLabel?.text = "\(brightness)%" } }else{ cell?.textLabel?.text = "\(self.colorValues[indexPath.row])k" } cell?.textLabel?.textAlignment = .center return cell! } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { //choseValue = self.dataSource?.values.first! if self.choseBtnTag == 1 { choseValue = self.brightnessValues[indexPath.row] if choseValue == "自动"{ leftBrightnessBtn?.setTitle("自动", for: .normal) }else{ leftBrightnessBtn?.setTitle("\(choseValue!)%", for: .normal) } }else{ choseValue = self.colorValues[indexPath.row] rightColorBtn?.setTitle("\(choseValue!)k", for: .normal) } log.debug("value: \(String(describing: choseValue))") //更新数据 NotificationCenter.default.post(name: NSNotification.Name(rawValue: KNOtificationClassLightReloadData), object: nil, userInfo: ["indexPath":self.indexPath!,"choseBtnTag": choseBtnTag,"choseValue" : choseValue!]) self.choseBtnTag = 0 self.valueBackView?.removeFromSuperview() } }