// // IHClassControlView.swift // Inhealth // // Created by weclouds on 2020/8/19. // Copyright © 2020 weclouds. All rights reserved. // import UIKit import PKHUD protocol IHClassControlViewDelegate : NSObjectProtocol{ //调色温与亮度 // func clickLight(_index : Int) // func clickLight(lightDevice : ShoolDeviceList) // //更换灯的位置 // func chargeLight(lists: [ShoolDeviceList]) // //窗帘的开关 // func curtainSetOnOff(curtainDevice : ShoolDeviceList,respone:@escaping ((_ msg : String)->Void)) // //色温 3000 - 6000 ,间隔是300 // func setClassLightColor(classDevices : [ShoolDeviceList]) // // //所有灯的开关 -- 模式类型(1 全开模式,2 全关模式,3 投影模式,4 自习模式,5 板书模式) // func lights_set_off(type : String) //ShoolDevice : ShoolDeviceList //点击跳转 func pushToLightCommandVC(ShoolDevice : (address : String ,type : String,location : String)) //面板情景id绑定按键id func panelBindScenesAndKeyId(panel : (address : String ,type : String,location : String),curtain : (address : String ,type : String,location : String)?,lampLights : [(address : String ,type : String,location : String)]?,classLights : [(address : String ,type : String,location : String)]?,scenes : String,keyId : String ) //操作情景模式 - 全关/全开 func switchScenesMode(panelAddress : String,lampLights : [(address : String ,type : String,location : String)],classLights : [(address : String ,type : String,location : String)], scenes : String ,keyId: String) //窗帘的开关 func controlCurtainTurnOn(panel : (address : String ,type : String,location : String),curtainDevice : (address : String ,type : String,location : String),scenes : String) //设置设备的位置 func setDeviceLocation(device : (address : String ,type : String,location : String)) //交换位置后再重新获取数据 func getDeviceData() } class IHClassControlView: UIView { //上课模型数组 var classModelArr = [UIView]() var lightsArr = [UIView]() var blackboardLights = [UIView]() var allLightsBtn : UIButton? var cancelBtn : UIButton? var sureBtn : UIButton? private var classImage : UIImageView? var blackboardLine : UIImageView? private var line : UIImageView? var moveLight : UIView? var isLongTap : Bool = false var isBlackboardLongTap : Bool = false var clickImage : UIView? var beginPoint = CGPoint.init(x: 0, y: 0) var originalPoint = CGPoint.init(x: 0, y: 0) var isChangeLight : Bool = false var recordLights = [[UIView : CGPoint]]() weak var delegate : IHClassControlViewDelegate? // private var classLightArr = [ShoolDeviceList]() // private var blackBoardLightArr = [ShoolDeviceList]() // private var curtainArr = [ShoolDeviceList]() private var classLightArr = [(address : String ,type : String,location : String)]() private var blackBoardLightArr = [(address : String ,type : String,location : String)]() private var curtainArr = [(address : String ,type : String,location : String)]() var selectBtnIndex : Int = 0 //var oldSelectBtnIndex : Int = 0 var isSuccess : Bool = false var isAllLightOpen : Bool = false var isSelect : Bool = false var blackBoardLightFrames : [(x : CGFloat,y :CGFloat,w : CGFloat,h : CGFloat)]? //黑板灯的位置 var classRoomLightFrames : [(x : CGFloat,y :CGFloat,w : CGFloat,h : CGFloat)]? //教室灯的位置 var deviceList : [(address : String ,type : String,location : String)]? var lampLights = [(address : String,type : String,location : String)]() var classLights = [(address : String,type : String,location : String)]() var curtainDevice = [(address : String,type : String,location : String)]() //添加成功还没有设置位置 var unSetClassLightLocation : [(address : String ,type : String,location : String)]? var unSetLampLightLocation : [(address : String ,type : String,location : String)]? var unSetCurtainLocation : [(address : String ,type : String,location : String)]? private var isFinish : Bool = false private var changeLocationCout = 0 // //数据 - 教室设备 // var shoolDivListArr : [ShoolDeviceList]? { // didSet{ // if classImage == nil { // createUI() // }else{ // classModelArr.removeAll() // lightsArr.removeAll() // blackboardLights.removeAll() // blackBoardLightFrames?.removeAll() // classRoomLightFrames?.removeAll() // allLightsBtn = nil // cancelBtn = nil // sureBtn = nil // classImage = nil // blackboardLine = nil // line = nil // moveLight = nil // isLongTap = false // isBlackboardLongTap = false // clickImage = nil // beginPoint = CGPoint.init(x: 0, y: 0) // originalPoint = CGPoint.init(x: 0, y: 0) // isChangeLight = false // recordLights = [[UIView : CGPoint]]() // // self.removeSubviews() // classLightArr.removeAll() // blackBoardLightArr.removeAll() // curtainArr.removeAll() // // createUI() // } // // } // } // private func resetUI(){ if classImage == nil { createUI() }else{ classModelArr.removeAll() lightsArr.removeAll() blackboardLights.removeAll() blackBoardLightFrames?.removeAll() classRoomLightFrames?.removeAll() allLightsBtn = nil cancelBtn = nil sureBtn = nil classImage = nil blackboardLine = nil line = nil moveLight = nil isLongTap = false isBlackboardLongTap = false clickImage = nil beginPoint = CGPoint.init(x: 0, y: 0) originalPoint = CGPoint.init(x: 0, y: 0) isChangeLight = false recordLights = [[UIView : CGPoint]]() self.removeSubviews() classLightArr.removeAll() blackBoardLightArr.removeAll() curtainArr.removeAll() lampLights.removeAll() classLights.removeAll() curtainDevice.removeAll() unSetClassLightLocation?.removeAll() unSetLampLightLocation?.removeAll() unSetCurtainLocation?.removeAll() createUI() } } override init(frame: CGRect) { super.init(frame: frame) NotificationCenter.default.addObserver(self, selector: #selector(updateAllDevices), name: NSNotification.Name(KNotifiGetAllDeviceInforFromGateway), object: nil) NotificationCenter.default.addObserver(self, selector: #selector(updateSceneMode), name: NSNotification.Name(KNotifiSwitchScenseMode), object: nil) //交换位置后,再重新数据 NotificationCenter.default.addObserver(self, selector: #selector(upadateDeviceLocation), name: NSNotification.Name(KNotifiSetLightLocation), object: nil) } override func layoutSubviews() { super.layoutSubviews() } @objc private func upadateDeviceLocation(){ //交换位置后,再重新数据 if isFinish == true { self.delegate?.getDeviceData() } } //模式换新 - 更新界面的状态 @objc private func updateSceneMode(noti : Notification){ let isfail = noti.userInfo?["isfail"] as? Bool let arr = noti.userInfo?["devicesInfor"] as? [(address : String ,type : String,location : String)] //更新模式的状态 if isfail == true { DispatchQueue.main.async { if arr != nil{ self.deviceList?.removeAll() self.deviceList = arr } self.resetUI() } } } //所有的设备 @objc private func updateAllDevices(noti : Notification){ let arr = noti.userInfo?["devicesInfor"] as! [(address : String ,type : String,location : String)] log.debug("---获取带地址的设备") self.deviceList?.removeAll() self.deviceList = arr DispatchQueue.main.async { if self.isFinish == true { self.changeLocationCout = self.changeLocationCout - 1 if self.changeLocationCout == 0 { self.resetUI() self.isFinish = false } }else{ //找出没有位置的 var unLocationDevice = [(address : String ,type : String,location : String)]() if arr.count > 0{ for device in arr{ if device.type != "12" && device.location == "00"{ unLocationDevice.append(device) } } } self.createUI() //设置所有设备的位置 - 如果有了就不设置 if unLocationDevice.count > 0{ for subDevice in unLocationDevice { for locationDevice in self.deviceList! { if subDevice.address == locationDevice.address { DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) { self.delegate?.setDeviceLocation(device: locationDevice) } } } } } } //self.isFinish = false } } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } deinit { log.debug("销毁--") NotificationCenter.default.removeObserver(self) } } extension IHClassControlView{ func createUI() { //设置大图片 // let backImageView = UIImageView.init(frame: self.bounds) let backImageView = UIImageView.init(frame: CGRect.init(x: 0, y: 0, width: KSCREENWIDTH + 2, height: self.frame.height)) backImageView.image = UIImage.init(named: "背景") self.addSubview(backImageView) //811.5 49.5 //教室背景图片 classImage = UIImageView.init() let percent = (10.5 * KSCREENWIDTH) / 376 classImage!.frame = CGRect.init(x: percent, y: KSCREENHEIGHT * 49.1 / 811.5 , width: KSCREENWIDTH - 2 * percent, height: self.frame.height - KSCREENHEIGHT * 49.1 / 811.5) classImage!.image = UIImage.init(named: "教室背景") classImage!.isUserInteractionEnabled = true self.addSubview(classImage!) // guard let deviceLists = self.shoolDivListArr else {return } // // for light in deviceLists { // //设备类型(4 教室灯,5 黑板灯,6 窗帘) // guard let type = light.type else { return} // if type == "4" { // classLightArr.append(light) // // } // if type == "5" { // blackBoardLightArr.append(light) // } // if type == "6" { // curtainArr.append(light) // } // } // for i in 0..<9 { // var list = ShoolDeviceList() // list.id = "\(i + 1)" // list.type = "\(i + 1)" // list.location = "\(i + 1)" // list.status = "1" // list.light = "100" // list.color = "6000" // list.name = "教室灯\(i + 1)" // list.model = "0" // classLightArr.append(list) // } // // for i in 0..<3 { // var list = ShoolDeviceList() // list.id = "\(i + 1)" // list.type = "\(i + 1)" // list.location = "\(i + 1)" // list.status = "1" // list.light = "100" // list.color = "6000" // list.name = "黑板灯\(i + 1)" // list.model = "0" // blackBoardLightArr.append(list) // } // // for i in 0..<2 { // var list = ShoolDeviceList() // list.id = "\(i + 1)" // list.type = "\(i + 1)" // list.location = "\(i + 1)" // list.status = "1" // list.light = "100" // list.color = "6000" // list.name = "窗帘\(i + 1)" // list.model = "0" // curtainArr.append(list) // } // //教室灯 // setClassLight(lights : classLightArr) // //黑板灯 // setBlackBoardLightLocation(lights : blackBoardLightArr) // // 窗帘 // setCurtain(curtains :curtainArr ) //假数据 // var devicesL = [(address : String ,type : String,location : String)]() // for i in 0..<3 { // let device = (address : "010\(i+1)",type : "04",location : "1\(i)") // devicesL.append(device) // } // for i in 0..<9 { // let device = (address : "010\(i+1)",type : "01",location : "2\(i)") // devicesL.append(device) // } // for i in 0..<2 { // let device = (address : "010\(i+1)",type : "40",location : "0\(i+1)") // devicesL.append(device) // } // self.deviceList = devicesL guard let devices = self.deviceList else { return } let allClassLightLocation = ["20","21","22","23","24","25","26","27","28"] let allLampLightLocation = ["10","11","12"] let allCurtainLocation = ["01","02"] //已用过的 var setClassLightLocation = [String]() var setLampLightLocation = [String]() var setCurtainLocation = [String]() var otherDevice = [(address : String,type : String,location : String)]() for subDevice in devices { //04: Lamp device(黑板灯) //31: sensor device(485)(教室灯) //01: CW Lamp (教室灯) //40: Curtain device (窗帘) if subDevice.type == "04" { if subDevice.location != "00"{ setLampLightLocation.append(subDevice.location) } lampLights.append(subDevice) self.deviceList?.removeAll(where: {$0 == subDevice}) }else if subDevice.type == "31" || subDevice.type == "01" { if subDevice.location != "00"{ setClassLightLocation.append(subDevice.location) } classLights.append(subDevice) self.deviceList?.removeAll(where: {$0 == subDevice}) }else if subDevice.type == "40" { if subDevice.location != "00"{ setCurtainLocation.append(subDevice.location) } curtainDevice.append(subDevice) //删除指定的元素 self.deviceList?.removeAll(where: {$0 == subDevice}) }else{ otherDevice.append(subDevice) } } //设置位置 if lampLights.count > 0 { setDeviceLocation(deviceList: lampLights, setDeviceLocation: setLampLightLocation, allDeviceLocation: allLampLightLocation, deviceType: "04") } if classLights.count > 0 { setDeviceLocation(deviceList: classLights, setDeviceLocation: setClassLightLocation, allDeviceLocation: allClassLightLocation, deviceType: "01") } if curtainDevice.count > 0{ setDeviceLocation(deviceList: curtainDevice, setDeviceLocation: setCurtainLocation, allDeviceLocation: allCurtainLocation, deviceType: "40") } //改变位置后的数组 self.deviceList = otherDevice + lampLights + classLights + curtainDevice //教室灯 setClassLight(lights : classLights) //黑板灯 setBlackBoardLightLocation(lights : lampLights) // 窗帘 setCurtain(curtains :curtainDevice ) // //开关与色温 setOnOffAndColTep() //上课模式 setClassModelUI() } //初始化位置 private func setDeviceLocation(deviceList : [(address : String,type : String,location : String)],setDeviceLocation : [String],allDeviceLocation : [String],deviceType : String ){ //设置位置 var totalLights = [(address : String,type : String,location : String)]() if setDeviceLocation.count > 0{ //未用过的 var unSetLocaiont = [String]() for location in allDeviceLocation { //找出还没用过的位置 var i = 0 for setLocation in setDeviceLocation { i = i + 1 if location != setLocation && i == setDeviceLocation.count { unSetLocaiont.append(location) } if location == setLocation { break } } } //设置位置 var i = 0 for light in deviceList { if light.location == "00"{ let subLight = (address : light.address,type : light.type,location : unSetLocaiont[i]) i = i + 1 totalLights.append(subLight) }else{ totalLights.append(light) } } }else{ //全部没用过 for ( index,light )in deviceList.enumerated() { var subLight : (address : String,type : String,location : String)? if deviceType == "04" { subLight = (address : light.address,type : light.type,location : "1\(index)") }else if deviceType == "40"{ subLight = (address : light.address,type : light.type,location : "0\(index + 1)") }else{ subLight = (address : light.address,type : light.type,location : "2\(index)") } totalLights.append(subLight!) } } // deviceList = totalLights if deviceType == "04"{ //黑板灯 lampLights = totalLights }else if(deviceType == "40"){ curtainDevice = totalLights }else{ classLights = totalLights } } //设置上课模式 private func setClassModel(classModelView_super : UIView) { let offsetX = (40 * KSCREENWIDTH) / 750 let width = (78 * KSCREENWIDTH) / 750 let between = (classModelView_super.size.width - offsetX * 2 - width * 5) / 5 // "午休", let arr = ["上课","休息","投影","自习","板书"] for i in (0..<5) { let backView = UIView.init(frame: CGRect.init(x: offsetX + CGFloat(i) * (width + between), y: 1, width: width, height: classModelView_super.size.height - 1)) backView.tag = i + 1 let line = UIView.init(frame: CGRect.init(x: 0, y: 0, width: width, height: 1)) line.backgroundColor = UIColor.init(hexString: "#3E328B") let btn = UIButton.init(frame: CGRect.init(x: 0, y: 1, width: width, height: backView.size.height)) //btn.setImage(UIImage.init(named: "勾选"), for: .normal) btn.titleLabel?.font = UIFont.systemFont(ofSize: 14) btn.setTitle(arr[i], for: .normal) btn.setTitleColor(.black, for: .normal) btn.isUserInteractionEnabled = false line.tag = tag + 1 btn.tag = tag + 1 backView.addSubview(line) backView.addSubview(btn) let tapGR = UITapGestureRecognizer.init(target: self, action: #selector(changeClassModel)) backView.addGestureRecognizer(tapGR) classModelView_super.addSubview(backView) line.backgroundColor = .white //btn.setImage(UIImage.init(named: ""), for: .normal) if selectBtnIndex != 0 { if i + 1 == selectBtnIndex { line.backgroundColor = UIColor.init(hexString: "#3E328B") btn.setImage(UIImage.init(named: "勾选"), for: .normal) } } btn.setTitleColor(UIColor.init(hexString: "#657085"), for: .normal) classModelArr.append(backView) } } //教室灯 ShoolDeviceList fileprivate func setLightUI(light : (address : String,type : String,location : String) ,offsetX : CGFloat,offsetY : CGFloat,backW : CGFloat,backH: CGFloat, lightW: CGFloat,lightH: CGFloat,light_superView: UIView){ // guard let location = light.location else { // return // } // guard let locationValue = Int(location) else{return} var locationValue : Int = 0 switch light.location { case "20": locationValue = 1 case "21": locationValue = 2 case "22": locationValue = 3 case "23": locationValue = 4 case "24": locationValue = 5 case "25": locationValue = 6 case "26": locationValue = 7 case "27": locationValue = 8 default: locationValue = 9 } let row : Int = (locationValue - 1) % 3 let col : Int = (locationValue - 1) / 3 let marginX = offsetX + CGFloat(row) * backW let marginY = offsetY + CGFloat(col) * backH //let X = String.init(format: "%.3f",marginX).cgFloat() let backLightView = UIView.init(frame: CGRect.init(x: Double(marginX), y: Double(marginY), width: Double(backW), height: Double(backH))) //灯 let x = (backW - lightW) * 0.5 let lightImageView = UIImageView.init(frame: CGRect.init(x: x, y: 0, width: lightW, height: lightH)) // guard let status = light.status else { // return // } // if status == "0" { // lightImageView.image = UIImage.init(named: "大灯_暗") // }else{ // lightImageView.image = UIImage.init(named: "大灯_亮") // } //灯亮的状态 let defaults = UserDefaults.standard let arrDevice = defaults.object(forKey: "saveDeviceList") as? [[String : String]] if let arrDevice = arrDevice { if arrDevice.count > 0 { for subDic in arrDevice { let address = subDic["address"]! let bright = subDic["bright"] if light.address == address && bright != "0"{ lightImageView.image = UIImage.init(named: "大灯_亮") break } if light.address == address && bright == "0"{ lightImageView.image = UIImage.init(named: "大灯_暗") break } } } }else{ //如果是别人先添加的设置,自己第一次运行 lightImageView.image = UIImage.init(named: "大灯_暗") } lightImageView.isUserInteractionEnabled = true backLightView.tag = locationValue lightImageView.tag = locationValue //标签 let lightLabel = UILabel.init(frame: CGRect.init(x: 0, y: lightH - 20, width: backW, height: backH - lightH + 10)) // lightLabel.text = light.name lightLabel.text = light.address lightLabel.textAlignment = .center lightLabel.textColor = .white lightLabel.font = UIFont.systemFont(ofSize: 12) lightLabel.isHidden = true self.clickImage = backLightView //手势 let tapGR = UITapGestureRecognizer.init(target: self, action: #selector(tapImage)) let longGR = UILongPressGestureRecognizer.init(target: self, action: #selector(longTapImage)) // backLightView.addGestureRecognizer(tapGR) // backLightView.addGestureRecognizer(longGR) lightImageView.addGestureRecognizer(tapGR) lightImageView.addGestureRecognizer(longGR) // light_superView.isUserInteractionEnabled = true backLightView.addSubview(lightImageView) backLightView.addSubview(lightLabel) light_superView.addSubview(backLightView) lightsArr.append(backLightView) } //设置窗帘 func setCurtainUI(curtain : (address : String,type : String,location : String), curtain_superView : UIView) { // // guard let location = curtain.location else { // return // } let curtain_offsetY = ((134 + 290) * KSCREENHEIGHT) / 1623 var offsetY : CGFloat = 0 if curtain.location == "02" { offsetY = curtain_offsetY } let curtainH = (290 * KSCREENHEIGHT) / 1623 let curtainBtn = UIButton.init(frame: CGRect.init(x: 0, y: offsetY, width: 30, height: curtainH)) // guard let status = curtain.status else { // return // } // if status == "1" { // //开 // curtainBtn.setImage(UIImage.init(named: "窗帘打开"), for: .normal) // }else{ // //关 // curtainBtn.setImage(UIImage.init(named: "窗帘关闭"), for: .normal) // } // guard let tag : Int = Int(location) else { // return // } let defaults = UserDefaults.standard let arrDevice = defaults.object(forKey: "saveDeviceList") as? [[String : String]] if let arrDevice = arrDevice { if arrDevice.count > 0 { for subDic in arrDevice { let address = subDic["address"]! let bright = subDic["bright"] if curtain.address == address && bright != "0"{ curtainBtn.setImage(UIImage.init(named: "窗帘打开"), for: .normal) break } if curtain.address == address && bright == "0"{ curtainBtn.setImage(UIImage.init(named: "窗帘关闭"), for: .normal) break } } } }else{ curtainBtn.setImage(UIImage.init(named: "窗帘关闭"), for: .normal) } // curtainBtn.setImage(UIImage.init(named: "窗帘关闭"), for: .normal) var tag = 0 if curtain.location == "01"{ tag = 1 }else{ tag = 2 } curtainBtn.tag = tag curtainBtn.addTarget(self, action: #selector(switchCurtain), for: .touchUpInside) curtain_superView.addSubview(curtainBtn) } } extension IHClassControlView{ //黑板灯-点击 @objc func blackboardTapImage(tapGR : UIGestureRecognizer){ let imageV = tapGR.view! log.debug("黑板-开关\(imageV.tag)") //教室灯长按之后,这里不允许进来 if isLongTap == true { return } guard let delegate = self.delegate else{ return } // for light in blackBoardLightArr { // guard let location = light.location else { // return // } // if location == String.init(format: "%d", imageV.tag){ // delegate.clickLight(lightDevice: light) // break // } // } for light in lampLights { var location = 0 switch light.location { case "10": location = 1 case "11": location = 2 default: location = 3 } if imageV.tag == location { delegate.pushToLightCommandVC(ShoolDevice: light) break } } //更改 // delegate.clickLight(_index: imageV.tag) } //黑板灯-长按 @objc func blackboardTLongTapImage(longTapGR : UIGestureRecognizer){ log.debug("黑板灯-长按灯了") if longTapGR.state == .began { for lightV in blackboardLights { lightV.isUserInteractionEnabled = false let dic = [lightV : lightV.frame.origin] recordLights.append(dic) for label in lightV.subviews { if label.isKind(of: UILabel.self) { label.isHidden = false } } } setLightRotation(lights: blackboardLights) isBlackboardLongTap = true //在黑板上盖一个 guard let coverView = line else { return } let blackview = UIView.init(frame: coverView.frame) blackview.tag = 100 blackview.backgroundColor = .clear classImage?.addSubview(blackview) } } //窗帘开关 @objc func switchCurtain(btn: UIButton){ log.debug("窗帘开关\(btn.tag)") btn.isSelected = !btn.isSelected if isBlackboardLongTap == true || isLongTap == true{ return } guard let delegate = delegate else { return } for device in self.deviceList! { //面板情景id绑定按键id //操作情景模式 if device.type == "12" { HUD.flash(.progress) if btn.isSelected == true { //开 delegate.panelBindScenesAndKeyId(panel: device, curtain: self.curtainDevice[btn.tag - 1], lampLights : nil,classLights : nil,scenes: "6", keyId: "5") }else{ delegate.panelBindScenesAndKeyId(panel: device, curtain: self.curtainDevice[btn.tag - 1], lampLights : nil,classLights : nil,scenes: "6", keyId: "5") } break } } for device in self.deviceList! { //面板情景id绑定按键id //操作情景模式 if device.type == "12" { delegate.controlCurtainTurnOn(panel: device, curtainDevice: self.curtainDevice[btn.tag - 1], scenes: "6") break } } // if btn.isSelected == true { // //开 // // // delegate.controlCurtainTurnOn(curtainDevice: self.curtainDevice[btn.tag - 1], scenes: "6") // //// delegate.panelBindScenesAndKeyId(panel: device, scenes: type, keyId: keyId) // }else{ // // delegate.controlCurtainTurnOn(curtainDevice: self.curtainDevice[btn.tag - 1], scenes: "6") // } // for curtain in curtainArr { // guard let location = curtain.location else { // return // } // if location == String.init(format: "%d", btn.tag){ // // delegate.curtainSetOnOff(curtainDevice: curtain, respone: { msg in // if msg == "successful operation"{ // //重新加载新的数据 // // } // }) // } // } } //面板灯开关 @objc func tapImage(tapGR : UIGestureRecognizer){ let imageV = tapGR.view! log.debug("面板灯-开关\(imageV.tag)") //黑板在移动里时,这里不允许点击 if isBlackboardLongTap == true { return } guard let delegate = self.delegate else{ return } // for light in classLightArr { // guard let location = light.location else { // return // } // if location == String.init(format: "%d", imageV.tag){ //// delegate.clickLight(lightDevice: light) // break // } // } for light in classLights { var location = 0 switch light.location { case "20": location = 1 case "21": location = 2 case "22": location = 3 case "23": location = 4 case "24": location = 5 case "25": location = 6 case "26": location = 7 case "27": location = 8 default: location = 9 } if imageV.tag == location { delegate.pushToLightCommandVC(ShoolDevice: light) break } } // delegate.pushToLightCommandVC(ShoolDevice: classLights[imageV.tag - 1]) // delegate.clickLight(_index: imageV.tag) } //面板灯长按 @objc func longTapImage(longGR : UIGestureRecognizer){ if longGR.state == .began { for lightV in lightsArr { lightV.isUserInteractionEnabled = false let dic = [lightV : lightV.frame.origin] recordLights.append(dic) for label in lightV.subviews { if label.isKind(of: UILabel.self) { label.isHidden = false } } } setLightRotation(lights: lightsArr) isLongTap = true //在黑板上盖一个 guard let coverView = blackboardLine else { return } let blackview = UIView.init(frame: coverView.frame) blackview.tag = 100 blackview.backgroundColor = .clear classImage?.addSubview(blackview) } } //设置抖动 func setLightRotation(lights : [UIView]) { let anim = CAKeyframeAnimation.init() anim.keyPath = "transform.rotation" //弧度转角度 let s = 4 / 180.0 * Double.pi // anim.values = [-s,0,s,0,-s,0,s,0] anim.values = [-s,s,-s] anim.repeatCount = MAXFLOAT anim.duration = 0.2 for lightView in lights { let views = lightView.subviews for subV in views { if subV.isKind(of: UIImageView.self) && subV.tag > 0 { subV.isUserInteractionEnabled = false subV.layer.add(anim, forKey: nil) } } } //隐藏-开关 allLightsBtn?.isHidden = true classModelArr[0].superview?.isHidden = true //显示--选中与取消 -- 注意 if blackboardLine != nil { blackboardLine!.isHidden = false } if line != nil{ line!.isHidden = false } cancelBtn?.isHidden = false sureBtn?.isHidden = false } //色温 @objc func changeColorTemp(tapGR : UIGestureRecognizer){ log.debug("色温") guard let delegate = delegate else { return } //教室灯循环请求,全部返回后再点击发命令 // delegate.setClassLightColor(classDevices: classLightArr) } //所有灯的开关 @objc func switchAll(btn : UIButton){ log.debug("所有灯的开关") btn.isSelected = !btn.isSelected //先要判断灯的状态 guard let delegate = delegate else { return } guard let lists = self.deviceList else { return } //灯的全开/全关 if btn.isSelected == true{ for device in lists { if device.type == "12"{ //配置情景id与按键id成功后再发送情景模式 HUD.flash(.progress) delegate.panelBindScenesAndKeyId(panel: device, curtain: nil,lampLights:lampLights,classLights:classLights, scenes: "1", keyId: "0") break } } }else{ for device in lists { if device.type == "12"{ //配置情景id与按键id成功后再发送情景模式 HUD.flash(.progress) delegate.panelBindScenesAndKeyId(panel: device, curtain: nil,lampLights:lampLights,classLights:classLights, scenes: "2", keyId: "1") break } } } // isSelect = !isSelect // if isSelect == true { //// delegate.lights_set_off(type: "1") // for device in lists { // if device.type == "12"{ // delegate.switchScenesMode(panelAddress: device.address, lampLights: lampLights, classLights: classLights, scenes: "1",keyId: "0") // //delegate.switchScenesMode(address : device.address, scenes : "2" ) // break // } // } // }else{ //// delegate.lights_set_off(type: "2") // // for device in lists { // if device.type == "12"{ // delegate.switchScenesMode(panelAddress: device.address, lampLights: lampLights, classLights: classLights, scenes: "1",keyId: "1") // //delegate.switchScenesMode(address : device.address, scenes : "2" ) // break // } // } // } } func setLightModel(blackboardLight : UIView, lightName : String) { for imageV in blackboardLight.subviews { if imageV.isKind(of: UIImageView.self) && imageV.tag > 0{ // imageV.isHighlighted = true let imageV = imageV as! UIImageView imageV.image = UIImage.init(named: lightName) } } } //改变上课模式 @objc func changeClassModel(tapGR : UIGestureRecognizer){ let view = tapGR.view! log.debug("模式:\(view.tag)") guard let lists = self.deviceList else { return } var count = 0 for device in lists { if device.type == "12"{ count = count + 1 } } if count == 0 { HUD.flash(.label("还没添加面板"), delay: 0.7) return } UIView.animate(withDuration: 0.3) { for subView in self.classModelArr { if view.tag == subView.tag { for v in subView.subviews { if(v.tag > 0){ if v.isMember(of: UIButton.self) { let btn :UIButton = v as! UIButton btn.setImage(UIImage.init(named: "勾选"), for: .normal) btn.setTitleColor(.black, for: .normal) }else{ v.backgroundColor = UIColor.init(hexString: "#3E328B") } } } }else if subView.tag > 0 && view.tag != subView.tag { for v in subView.subviews { if(v.tag > 0){ if v.isMember(of: UIButton.self) { let btn :UIButton = v as! UIButton //btn.setImage(UIImage.init(named: ""), for: .normal) btn.setTitleColor(UIColor.init(hexString: "#657085"), for: .normal) }else{ v.backgroundColor = .white } } } } } } guard let delegate = delegate else { return } // 模式类型(1 全开模式,2 全关模式,3 投影模式,4 自习模式,5 板书模式,7 上课模式,8 下课模式,9 午休模式) //1-7 2-8 3-9 4-3 5-4 6-5 // var type = "1" // if view.tag == 1 { // type = "7" // } // if view.tag == 2 { // type = "8" // } // if view.tag == 3 { // type = "9" // } // if view.tag == 4 { // type = "3" // } // if view.tag == 5 { // type = "4" // } // if view.tag == 6 { // type = "5" // } // 全开模式 1,全关模式 2,投影模式 3,自习模式 4,板书模式 5,窗帘开/关 6,上课模式 7,下课模式 8 var type = "1" var keyId = "1" if view.tag == 1 { type = "7" keyId = "0" } if view.tag == 2 { type = "8" keyId = "4" } if view.tag == 3 { type = "2" keyId = "1" } if view.tag == 4 { type = "3" keyId = "3" } if view.tag == 5 { type = "4" keyId = "2" } selectBtnIndex = view.tag /** 按键1 : 上课模式 按键2 : 投影模式 按键3 : 板书模式 按键4 : 自习模式 按键5 : 休息模式 按键6 : 窗帘开关 */ for device in lists { if device.type == "12"{ //配置情景id与按键id成功后再发送情景模式 HUD.flash(.progress) delegate.panelBindScenesAndKeyId(panel: device, curtain: nil,lampLights:lampLights,classLights:classLights, scenes: type, keyId: keyId) break } } // delegate.lights_set_off(type: type) } @objc func cancelChooseClass(btn: UIButton){ //取消选中教室 - 面板灯又回到原来的位置 if isLongTap == true{ var arr = [UIView]() for dic in recordLights { let key = dic.keys.first let value = dic.values.first key!.frame.origin = value! arr.append(key!) } lightsArr = arr //取消黑板灯的蒙板 let coverView = classImage?.viewWithTag(100) coverView?.removeFromSuperview() } if isBlackboardLongTap == true { var arr = [UIView]() for dic in recordLights { let key = dic.keys.first let value = dic.values.first key!.frame.origin = value! arr.append(key!) } blackboardLights = arr //取消教室灯的蒙板 let coverView = classImage?.viewWithTag(100) coverView?.removeFromSuperview() } setBottomState() } @objc func chooseClass(btn: UIButton){ guard let delegate = delegate else { return } //更换 -- 灯的位置 var lights = [(address : String, type : String, location : String)]() if isBlackboardLongTap == true { //黑板灯 //原来的位置与改变后的位置相比 for (idx,changeLocation) in blackboardLights.enumerated() { let light = lampLights[idx] //位置 : 0x10,0x11,0x12 var locationValue = 0 switch light.location { case "10": locationValue = 1 case "11": locationValue = 2 default: locationValue = 3 } if changeLocation.tag != locationValue { var lastLocation = "0" switch changeLocation.tag { case 1: lastLocation = "10" case 2: lastLocation = "11" default: lastLocation = "12" } lights.append((address : light.address,type : light.type,location : lastLocation)) } // guard let loc = light.location else { // return // } // guard let locValue = Int(loc) else { // return // } // if changeLocation.tag != locValue{ // light.location = String.init(format: "%d", changeLocation.tag) // lights.append(light) // } } } if isLongTap == true { //教室灯 //原来的位置与改变后的位置相比 for (idx,changeLocation) in lightsArr.enumerated() { let light = classLights[idx] //位置 : 0x10,0x11,0x12 var locationValue = 0 switch light.location { case "20": locationValue = 1 case "21": locationValue = 2 case "22": locationValue = 3 case "23": locationValue = 4 case "24": locationValue = 5 case "25": locationValue = 6 case "26": locationValue = 7 case "27": locationValue = 8 default: locationValue = 9 } if changeLocation.tag != locationValue { var lastLocation = "0" switch changeLocation.tag { case 1: lastLocation = "20" case 2: lastLocation = "21" case 3: lastLocation = "22" case 4: lastLocation = "23" case 5: lastLocation = "24" case 6: lastLocation = "25" case 7: lastLocation = "26" case 8: lastLocation = "27" default: lastLocation = "28" } lights.append((address : light.address,type : light.type,location : lastLocation)) } // guard let loc = light.location else { // return // } // guard let locValue = Int(loc) else { // return // } //// if changeLocation.tag == locValue{ //// return //// } // if changeLocation.tag != locValue{ // light.location = String.init(format: "%d", changeLocation.tag) // lights.append(light) // } } } var i = 0 for subLight in lights { i = i + 1 if i == lights.count{ isFinish = true changeLocationCout = i } DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) { delegate.setDeviceLocation(device: subLight) } } // delegate.chargeLight(lists: lights) //选中教室 setBottomState() } func setBottomState(){ //取消教室灯的蒙板 let coverView = classImage?.viewWithTag(100) coverView?.removeFromSuperview() if(isLongTap == true){ for lightV in lightsArr { lightV.isUserInteractionEnabled = true for imageView in lightV.subviews { if imageView.isKind(of: UIImageView.self) && imageView.tag > 0 { imageView.isUserInteractionEnabled = true imageView.layer.removeAllAnimations() } if imageView.isKind(of: UILabel.self) { imageView.isHidden = true } } } } if(isBlackboardLongTap == true){ for lightV in blackboardLights { lightV.isUserInteractionEnabled = true for imageView in lightV.subviews { if imageView.isKind(of: UIImageView.self) && imageView.tag > 0 { imageView.isUserInteractionEnabled = true imageView.layer.removeAllAnimations() } if imageView.isKind(of: UILabel.self) { imageView.isHidden = true } } } } // --- 注意 if blackboardLine != nil { blackboardLine!.isHidden = true } if line != nil { line!.isHidden = true } cancelBtn?.isHidden = true sureBtn?.isHidden = true allLightsBtn?.isHidden = false classModelArr[0].superview?.isHidden = false isLongTap = false isBlackboardLongTap = false } } //上课模式 extension IHClassControlView{ func setClassModelUI() { //上课模式 // let classY = (1321 * KSCREENHEIGHT) / 811.5 // let classH = (82 * KSCREENHEIGHT) / 811.5 let classY = (632 * KSCREENHEIGHT) / 811.5 let classH = (41 * KSCREENHEIGHT) / 811.5 let classModelView = UIView.init(frame: CGRect.init(x: 0, y: classY , width: classImage!.size.width, height: classH)) classModelView.backgroundColor = .white classModelView.cornerRadius = 8 classModelView.layer.masksToBounds = true classImage!.addSubview(classModelView) setClassModel(classModelView_super: classModelView) //选中界面下的 x和v let btnX = (254 * KSCREENWIDTH) / 750 let btnY = (1311 * KSCREENHEIGHT) / 1623 let btnWH = (60 * KSCREENWIDTH) / 750 cancelBtn = UIButton.init(frame: CGRect.init(x: btnX, y: btnY - 5, width: btnWH, height: btnWH)) cancelBtn?.setImage(UIImage.init(named: "取消"), for: .normal) cancelBtn?.backgroundColor = UIColor.init(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 0.7) cancelBtn?.addTarget(self, action: #selector(cancelChooseClass), for: .touchUpInside) cancelBtn?.cornerRadius = btnWH / 2 cancelBtn?.layer.masksToBounds = true cancelBtn?.isHidden = true classImage!.addSubview(cancelBtn!) let sbtnX = (394 * KSCREENWIDTH) / 750 sureBtn = UIButton.init(frame: CGRect.init(x: sbtnX, y: btnY - 5, width: btnWH, height: btnWH)) sureBtn?.setImage(UIImage.init(named: "确认"), for: .normal) sureBtn?.backgroundColor = UIColor.init(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 0.7) sureBtn?.addTarget(self, action: #selector(chooseClass), for: .touchUpInside) sureBtn?.isHidden = true sureBtn?.cornerRadius = btnWH / 2 sureBtn?.layer.masksToBounds = true classImage!.addSubview(sureBtn!) } } //开关与色温 extension IHClassControlView{ func setOnOffAndColTep() { //所有灯的开关 let lightsX = (9.5 * KSCREENWIDTH) / 376 let lightsY = (582 * KSCREENHEIGHT) / 811.5 let lightsW = (35 * KSCREENWIDTH) / 376 allLightsBtn = UIButton.init(frame: CGRect.init(x: lightsX, y: lightsY, width: lightsW, height: lightsW)) allLightsBtn?.isSelected = isSelect if isSelect == false{ allLightsBtn?.setBackgroundImage(UIImage.init(named: "全关"), for: .normal) allLightsBtn?.setBackgroundImage(UIImage.init(named: "全开"), for: .selected) }else{ allLightsBtn?.setBackgroundImage(UIImage.init(named: "全开"), for: .normal) allLightsBtn?.setBackgroundImage(UIImage.init(named: "全关"), for: .selected) } allLightsBtn?.addTarget(self, action: #selector(switchAll), for: .touchUpInside) classImage!.addSubview(allLightsBtn!) //色温开关 let tempX = (309 * KSCREENWIDTH) / 376 let tempImageV = UIImageView.init(frame: CGRect.init(x: tempX, y: lightsY, width: lightsW, height: lightsW)) tempImageV.image = UIImage.init(named: "色温") let tapGR = UITapGestureRecognizer.init(target: self, action: #selector(changeColorTemp)) tempImageV.addGestureRecognizer(tapGR) tempImageV.isUserInteractionEnabled = true //暂时取消 //classImage!.addSubview(tempImageV) } } //教室灯 extension IHClassControlView{ //[ShoolDeviceList] fileprivate func setClassLight(lights :[(address : String,type : String,location : String)]){ if lights.count == 0 { return } //面板灯的网格线 let lineX = (49 * KSCREENWIDTH) / 750 let lineY = (222 * KSCREENHEIGHT) / 1623 let lineH = (1029 * KSCREENHEIGHT) / 1623 line = UIImageView.init(frame: CGRect.init(x: lineX, y: lineY, width: classImage!.size.width - 2 * lineX, height: lineH)) line!.image = UIImage.init(named: "3-3网格") line!.isHidden = true classImage!.addSubview(line!) //面板灯222 let offsetY = (202 * KSCREENHEIGHT) / 1623 let offsetX = (49 * KSCREENWIDTH) / 750 let backW = ((708 - 49 * 2) / 3 * KSCREENWIDTH) / 750 let backH = ((1029 * KSCREENHEIGHT) / 1623) / 3 let lightW : CGFloat = (120 * KSCREENWIDTH) / 750 let lightH = (306 * KSCREENHEIGHT) / 1623 //计算所有按键的位置 setAllClassRoomLightFrame(offsetX: offsetX, offsetY: offsetY, backW: backW, backH: backH) for light in lights { setLightUI(light : light,offsetX : offsetX, offsetY : offsetY, backW : backW,backH: backH, lightW: lightW,lightH: lightH,light_superView: classImage!) } // for light in lights { // // setLightUI(light : light,offsetX : offsetX, offsetY : offsetY, backW : backW,backH: backH, lightW: lightW,lightH: lightH,light_superView: classImage!) // } } } //计算所有的灯的位置 extension IHClassControlView { //设置教室灯的位置 private func setAllClassRoomLightFrame (offsetX : CGFloat, offsetY : CGFloat, backW : CGFloat,backH: CGFloat){ var frames = [(x:CGFloat,y:CGFloat,w:CGFloat,h:CGFloat)]() for i in 0..<9 { let row : Int = i % 3 let col : Int = i / 3 let marginX = offsetX + CGFloat(row) * backW let marginY = offsetY + CGFloat(col) * backH let X = String.init(format: "%.3f", marginX).cgFloat() frames.append((x: X!, y: marginY, w: backW, h: backH)) } classRoomLightFrames = frames } private func setBlackBoardLightFrames(width: CGFloat,height : CGFloat) { var frames = [(x:CGFloat,y:CGFloat,w:CGFloat,h:CGFloat)]() for i in 0..<3 { var offsetX : CGFloat = 102 offsetX = offsetX + width * CGFloat(i) let light_percent = (offsetX * KSCREENWIDTH) / 750 let width = (width * KSCREENWIDTH) / 750 let X = String.init(format: "%.3f", light_percent).cgFloat() frames.append((x : X!, y: 0, w : width, h : height)) } blackBoardLightFrames = frames } } //窗帘 extension IHClassControlView{ //[ShoolDeviceList] func setCurtain(curtains : [(address : String,type : String,location : String)]) { if curtains.count > 0 { //窗帘 360 let curtain_percent = (360 * KSCREENHEIGHT) / 1623 let height = ((290 * 2 + 134) * KSCREENHEIGHT) / 1623 let curtainView = UIView.init(frame: CGRect.init(x: 0, y: curtain_percent, width: 44, height: height)) curtainView.backgroundColor = .clear classImage!.addSubview(curtainView) for curtain in curtains { setCurtainUI(curtain : curtain, curtain_superView: curtainView) } } } } //黑板灯 extension IHClassControlView { //[ShoolDeviceList] fileprivate func setBlackBoardLightLocation(lights : [(address : String,type : String,location : String)]) { if lights.count == 0 { return } //黑板灯的网格线 let blackboardLineX = (102 * KSCREENWIDTH) / 750 let blackboardLineH = (136 * KSCREENHEIGHT) / 1623 blackboardLine = UIImageView.init(frame: CGRect.init(x: blackboardLineX, y: 0, width: classImage!.size.width - 2 * blackboardLineX, height: blackboardLineH)) blackboardLine!.image = UIImage.init(named: "1-3网格") blackboardLine!.isHidden = true classImage!.addSubview(blackboardLine!) let w : CGFloat = (708 - 102 * 2) / 3 //计算所有的黑板灯的位置 setBlackBoardLightFrames(width: w,height : blackboardLineH) for light in lights { setBlackBoardLight(light : light,width: w,height : blackboardLineH,blackBoardLight_superView: classImage!) } } //设置黑板灯 //ShoolDeviceList fileprivate func setBlackBoardLight(light : (address : String,type : String,location : String),width: CGFloat,height : CGFloat, blackBoardLight_superView : UIView) { // guard let location = light.location else { // return // } var offsetX : CGFloat = 102 if light.location == "11" { offsetX = offsetX + width } if light.location == "12" { offsetX = offsetX + width * 2 } let light_percent = (offsetX * KSCREENWIDTH) / 750 let width = (width * KSCREENWIDTH) / 750 //let blackboardView = UIView.init(frame: CGRect.init(x: light_percent, y: 0, width: width, height: height)) let X = String.init(format: "%.3f",light_percent).cgFloat() let blackboardView = UIView.init(frame: CGRect.init(x: X!, y: 0, width: width, height: height)) //灯 // guard let status = light.status else { // return // } // let blackboard_light = UIImageView.init() // if status == "0" { // blackboard_light.image = UIImage.init(named: "黑板灯_暗") // }else{ // blackboard_light.image = UIImage.init(named: "黑板灯_亮") // } let blackboard_light = UIImageView.init() //灯亮的状态 let defaults = UserDefaults.standard let arrDevice = defaults.object(forKey: "saveDeviceList") as? [[String : String]] if let arrDevice = arrDevice { if arrDevice.count > 0 { for subDic in arrDevice { let address = subDic["address"]! let bright = subDic["bright"] if light.address == address && bright != "0"{ blackboard_light.image = UIImage.init(named: "黑板灯_亮") break } if light.address == address && bright == "0"{ blackboard_light.image = UIImage.init(named: "黑板灯_暗") break } } } }else{ blackboard_light.image = UIImage.init(named: "黑板灯_暗") } blackboard_light.sizeToFit() blackboard_light.frame.origin = CGPoint.init(x: (blackboardView.frame.size.width - blackboard_light.frame.size.width) * 0.5, y: -10) // guard let tag = Int(location) else { // return // } var tag = 0 if light.location == "10"{ tag = 1 }else if light.location == "11"{ tag = 2 }else{ tag = 3 } blackboardView.tag = tag blackboard_light.tag = tag //标签 let blackboardLabel = UILabel.init(frame: CGRect.init(x: 0, y: 30, width: width, height: 34)) // blackboardLabel.text = light.name blackboardLabel.text = light.address blackboardLabel.textColor = .white blackboardLabel.font = UIFont.systemFont(ofSize: 12) blackboardLabel.textAlignment = .center blackboardLabel.isHidden = true blackboardView.addSubview(blackboard_light) blackboardView.addSubview(blackboardLabel) blackBoardLight_superView.addSubview(blackboardView) //手势 let tapGR = UITapGestureRecognizer.init(target: self, action: #selector(blackboardTapImage)) let longGR = UILongPressGestureRecognizer.init(target: self, action: #selector(blackboardTLongTapImage)) blackboardView.addGestureRecognizer(tapGR) blackboardView.addGestureRecognizer(longGR) self.clickImage = blackboardView blackboardLights.append(blackboardView) } } extension IHClassControlView { override func touchesBegan(_ touches: Set, with event: UIEvent?) { //长按之后 if isLongTap == true { lightTouch(touches: touches, lights: lightsArr) } if isBlackboardLongTap == true { lightTouch(touches: touches, lights: blackboardLights) } } override func touchesMoved(_ touches: Set, with event: UIEvent?) { if !beginPoint.equalTo(CGPoint.init(x: 0, y: 0)) { if isLongTap == true{ lightMove(touches: touches) } if isBlackboardLongTap == true { lightMove(touches: touches) } } } override func touchesEnded(_ touches: Set, with event: UIEvent?) { log.debug("结束--") if !beginPoint.equalTo(CGPoint.init(x: 0, y: 0)){ if isLongTap == true{ lightEnd(touches: touches, lights: lightsArr) } if isBlackboardLongTap == true { lightEnd(touches: touches, lights: blackboardLights) } } } //面板灯的 - 点击 func lightTouch(touches: Set,lights : [UIView]){ //遍历所有的面板灯的坐标 for touch:AnyObject in touches { let touche : UITouch = touch as! UITouch let point = touche.location(in: classImage) for lightView in lights { let bool = lightView.frame.contains(point) if bool == true { classImage!.bringSubviewToFront(lightView) beginPoint = point moveLight = lightView originalPoint = lightView.frame.origin } } } } //面板灯 - 移动 func lightMove(touches: Set) { for touch:AnyObject in touches { let touche : UITouch = touch as! UITouch let point = touche.location(in: classImage) let x = point.x - beginPoint.x let y = point.y - beginPoint.y guard let moveLight = moveLight else { return } moveLight.center.x = moveLight.center.x + x moveLight.center.y = moveLight.center.y + y beginPoint = point } } //灯 - 停止 func lightEnd(touches: Set,lights : [UIView]) { for touch:AnyObject in touches { let touche : UITouch = touch as! UITouch let endPoint = touche.location(in: classImage) //1.先遍历点是否包含在frame的数组中 //2.1. 如果移动的终点是没有灯的,就直接修改该灯的位置 //2.2. 如果移动的终点是有灯的,就与此灯交换位置 var isContain = false var contained = false var lightFrames = [(x : CGFloat,y :CGFloat,w : CGFloat,h : CGFloat)]() if isLongTap == true { lightFrames = self.classRoomLightFrames! } if isBlackboardLongTap == true { lightFrames = self.blackBoardLightFrames! } for (index,frameTupe) in lightFrames.enumerated() { let frame = CGRect.init(x: frameTupe.x, y: frameTupe.y, width: frameTupe.w, height: frameTupe.h) isContain = frame.contains(endPoint) let frameX = String.init(format: "%.3f", frame.origin.x) let frameY = String.init(format: "%.3f", frame.origin.y) //再看是否有可以交换的灯 if isContain == true{ contained = isContain var ischange = 0 for light in lights { //&& light.frame != moveLight!.frame let lightFrameX = String.init(format: "%.3f", light.frame.origin.x) let lightFrameY = String.init(format: "%.3f", light.frame.origin.y) //创建view精度会改变,所以把origin取二位做比较 if frameX == lightFrameX && frameY == lightFrameY { //有交换的灯,但不能交换自己 log.debug("有交换的\(frame),\(light.frame)") ischange = 1 light.frame.origin = originalPoint light.tag = moveLight!.tag moveLight!.frame = frame moveLight!.tag = index + 1 beginPoint = CGPoint.init(x: 0, y: 0) break } } if ischange == 0 { //没有交换的,直接修改位置 log.debug("直接修改位置\(frame)") moveLight?.tag = index + 1 moveLight?.frame = frame beginPoint = CGPoint.init(x: 0, y: 0) break } } } if isContain == false && contained == false{ //结束点是否在 这些 log.debug("不在classRoomFrame范围内\(frame)") moveLight?.frame.origin = originalPoint beginPoint = CGPoint.init(x: 0, y: 0) break } // for lightView in lights{ // let bool = lightView.frame.contains(endPoint) // log.debug("包含:\(bool),\(lightView)" ) // if bool == true { // // if lightView.isEqual(moveLight) { // //2.如果不挡住就回原来的位置 // lightView.frame.origin = originalPoint // beginPoint = CGPoint.init(x: 0, y: 0) // }else{ // // //1.如果挡住就置换 // let end = lightView.frame.origin // lightView.frame.origin = originalPoint // moveLight?.frame.origin = end // // //更换location // let location = lightView.tag // guard let moveLight = moveLight else { // return // } // lightView.tag = moveLight.tag // moveLight.tag = location // beginPoint = CGPoint.init(x: 0, y: 0) // // break // } // } // } } } }