123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409 |
- //
- // IHLightBrightView.swift
- // Inhealth
- //
- // Created by weclouds on 2020/8/21.
- // Copyright © 2020 weclouds. All rights reserved.
- //
- import UIKit
- import PKHUD
- class IHLightBrightView: UIView {
- var lampControlTrunoff:(()->Void)?
- var colorTempCallback:((String)->Void)?
- var brightnessCallback:((String)->Void)?
- fileprivate var gradientLayer :IHGradientLayer?
- @IBOutlet weak var brightnessBtn: UIButton!
- @IBOutlet weak var colorTempBtn: UIButton!
- @IBOutlet weak var touchView: UIImageView!
- @IBOutlet weak var brightThumb: UIImageView!
- // @IBOutlet weak var colorPickerView: UIView!
- @IBOutlet weak var brightnessPickerView: UIView!
-
- @IBOutlet weak var whitePickerView: UIView!
- @IBOutlet weak var whiteThumb: UIImageView!
- @IBOutlet weak var lampInfoBox: UIView!
-
- var isVertical :Bool! = true //判断是水平滑动还是垂直滑动
- var isMove :Bool! = false //判断手指是否移动了
-
- var lightMode:String?
- var model : String = "" //学校灯为1 : EL-DP03-1200-LY时,不支持调色调
-
- var isVerticalMove :Bool! = false // 纵轴移动
- {
- didSet{
- if self.isVerticalMove == true {
- self.whitePickerView.isHidden = true
- self.whiteThumb.isHidden = true
- self.brightnessPickerView.isHidden = false
- self.brightThumb.isHidden = false
- }else {
- self.whitePickerView.isHidden = true
- self.whiteThumb.isHidden = true
- self.brightnessPickerView.isHidden = true
- self.brightThumb.isHidden = true
- }
- }
- }
- var isHorizontalMove :Bool! = false //横轴移动
- {
- didSet{
- if self.isHorizontalMove == true {
- self.whitePickerView.isHidden = false
- self.whiteThumb.isHidden = false
- self.brightnessPickerView.isHidden = true
- self.brightThumb.isHidden = true
- }else {
- self.whitePickerView.isHidden = true
- self.whiteThumb.isHidden = true
- self.brightnessPickerView.isHidden = true
- self.brightThumb.isHidden = true
- }
- }
- }
-
- var isLock:Bool? = false //加一把锁 保证单独执行
- var horizontalSensitivity : CGFloat = 30 //水平灵敏度
- let sensitivity : CGFloat = 50 //灵敏度 移动的又有效距离 / sensitivity
- let whitePickerWidth = KSCREENWIDTH - 80
- let beginY :CGFloat? = (KSCREENHEIGHT - 400) / 2
- let endY :CGFloat? = (KSCREENHEIGHT - 400) / 2 + 400
- var beginPoint : CGPoint?
- var movePoint : CGPoint?
- var colors :[CGColor]?{
- didSet{
- // setNeedsDisplay()
- if let colors = self.colors {
-
- if colors.count == 2 {
- self.gradientLayer?.colors = colors
- }else{
- let firstColor = UIColor(red: 1, green: 0, blue: 0, alpha: 0.12).cgColor
- let secondColor = UIColor(red: 1, green: 0, blue: 0, alpha: 1).cgColor
- self.gradientLayer?.colors = [firstColor,secondColor]
- }
-
- }
- }
- }
-
- var colorTemperature : Int?{
- didSet{
-
- }
- }
-
- var finalBrightness :String?
- var finalColorTemperature:String?
- var currentBrightness:String?{
- didSet{
- if let currentBrightness = self.currentBrightness {
- let bri: Int! = Int(currentBrightness)
- self.bright = endY! - CGFloat(bri) / 100 * 400
- // finalBrightness = currentBrightness
- }
- }
- }
-
- var currentColorTemp : String?{
- didSet{
- if let currentColorTemp = self.currentColorTemp {
- let colorTemp : Int! = Int(currentColorTemp)
- self.tvalue = (CGFloat(colorTemp) - 3000) / 3000 * whitePickerWidth + 40
- // finalColorTemperature = currentColorTemp
- }
- }
- }
-
-
- var saturation :CGFloat?
- var tvalue:CGFloat? {//色温的横坐标
- didSet{
- if let tvalue = self.tvalue {
- self.whiteThumb.center.x = tvalue
- self.colorTemperature = Int((tvalue - 40) / whitePickerWidth * 3000) + 3000
- self.colorTempBtn.setTitle("\(colorTemperature ?? 3000)k", for: .disabled)
- self.finalColorTemperature = "\(colorTemperature ?? 3000)"
- saturation = (tvalue - 40) / whitePickerWidth
- let aSaturation = saturation! * 0.7 + 0.3
- let aBrigghtness = (self.brightness ?? 1) * 0.8
- let color = UIColor(hue: hvalue, saturation: aSaturation , brightness: aBrigghtness, alpha: 1)
- setViewColor(color)
- }
- }
- }
- //颜色亮度
- var brightness :CGFloat?
- var bright : CGFloat?{
- didSet{
- if let bright = self.bright {
- self.brightThumb.center.y = (bright )
- let result = (400 - (bright - beginY!) ) / 400 * 100
- self.brightnessBtn.setTitle("\(Int(result))%", for: .disabled)
- self.finalBrightness = "\(Int(result))"
- if result == 0 {
- if let block = self.lampControlTrunoff {
- block()
- }
- }
- self.brightness = result / 100
- setColorBrightness(result / 100)
-
- }
-
- }
- }
- var hvalue :CGFloat!{
- return getHvalue(UIColor(hexString: "#FCD149"))
- }
- override func awakeFromNib() {
- super.awakeFromNib()
- gradientLayer = IHGradientLayer()
- gradientLayer?.frame = self.bounds
- self.layer.insertSublayer(gradientLayer!, at: 0)
- // beginY = self.brightnessPickerView.frame.minY
- // endY = self.brightnessPickerView.frame.maxY
- setViewColor(UIColor(hexString: "#FCD149"))
- setupUI()
- }
-
-
- func setupUI() {
- brightnessBtn.layoutButton(style: .Left, imageTitleSpace: 10)
- colorTempBtn.layoutButton(style: .Left, imageTitleSpace: 10)
- let bgLayer1 = CAGradientLayer()
- bgLayer1.colors = [UIColor.white.cgColor, UIColor(red: 236/255, green: 168/255, blue: 98/255, alpha: 1).cgColor]
- bgLayer1.locations = [0, 1]
- bgLayer1.frame = whitePickerView.bounds
- bgLayer1.startPoint = CGPoint(x: 0, y: 0)
- bgLayer1.endPoint = CGPoint(x: 1, y: 0)
- whitePickerView.layer.addSublayer(bgLayer1)
-
-
- self.brightThumb.center.y = self.brightnessPickerView.frame.minY
- self.bright = self.brightThumb.center.y
- self.whiteThumb.center.x = self.whitePickerView.frame.minX
- self.tvalue = self.whiteThumb.center.x
- }
-
- // override func draw(_ rect: CGRect) {
-
- // let colors = [UIColor.white.cgColor,UIColor.blue.cgColor]
- // if let cgColor = self.colors {
- // let endRadius = sqrt(pow(frame.width/2, 2) + pow(frame.height/2, 2))
- // let center = CGPoint(x: bounds.size.width / 2, y: bounds.size.height / 2)
- // let gradient = CGGradient(colorsSpace: nil, colors: cgColor as! CFArray, locations: nil)
- // let context = UIGraphicsGetCurrentContext()
- //
- // context?.drawRadialGradient(gradient!, startCenter: center, startRadius: 0.0, endCenter: center, endRadius: endRadius, options: CGGradientDrawingOptions.drawsBeforeStartLocation)
- // }
- // }
- }
- extension IHLightBrightView{
- override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
- let touch : UITouch! = touches.first
- self.beginPoint = touch.location(in: self)
- self.touchView.isHidden = false
- self.touchView.center = self.beginPoint!
-
-
- }
- override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
- reset()
- }
-
- override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
- reset()
- }
-
- override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
-
- let touch : UITouch! = touches.first
- self.movePoint = touch.location(in: self)
- let previousPoint = touch.previousLocation(in: self)
- let directX = movePoint!.x - previousPoint.x
- let directY = movePoint!.y - previousPoint.y
-
- self.touchView.isHidden = false
- self.lampInfoBox.isHidden = false
- if self.lightMode == "0" {
- self.colorTempBtn.isHidden = true
-
- }
- self.touchView.center = self.movePoint!
- let height = movePoint!.y - beginPoint!.y
- let width = beginPoint!.x - movePoint!.x
- let rads = atan(height/width);
- let degrees = 180.0 * Double(rads) / .pi
- let deltaDegrees = abs(degrees) //角度的d绝对值
- log.debug(deltaDegrees)
- if deltaDegrees.isNaN == false {//结果为nan的时候 不计算
- if deltaDegrees < 45 {
- if isLock == false { //如果是开锁 则可以进来判断是否可以移动 ,
- if self.model == "1" {
- //学校灯
- HUD.flash(.label("该型号不支持调色温"), delay: 1)
- return;
- }
- isLock = true //进来之后就上锁
- isHorizontalMove = true
- isVerticalMove = false
- }
- }else{
- if isLock == false {
- isLock = true
- isHorizontalMove = false
- isVerticalMove = true
- }
- }
- }
- if isHorizontalMove == true{ // 水平滑动
-
- isMove = true
- isVertical = false
- self.brightThumb.isHidden = true
- self.brightnessBtn.isHidden = true
- self.brightnessPickerView.isHidden = true
- if self.lightMode != "0" {
- self.whitePickerView.isHidden = false
- self.whiteThumb.isHidden = false
- self.colorTempBtn.isHidden = false
-
- tvalue! += (directX )
- if tvalue! < 40 {
- tvalue = self.whitePickerView.frame.maxX //循环话就跳到尾部
- //tvalue = 40 //不循环就就恢复起点
- }
- if tvalue! > (self.whitePickerView.frame.maxX) {
- tvalue = 40
- // tvalue = self.whitePickerView.frame.maxX
- }
-
- }
- }
-
- if isVerticalMove == true { //垂直移动
- isMove = true
- isVertical = true
- self.whitePickerView.isHidden = true
- self.whiteThumb.isHidden = true
- self.colorTempBtn.isHidden = true
- self.brightnessBtn.isHidden = false
- self.brightnessPickerView.isHidden = false
- self.brightThumb.isHidden = false
-
- bright! += (directY ) //弹性系数
- if bright! < beginY!{
- bright = beginY
- }
- if bright! > endY!{
- bright = endY
- }
-
- }
-
- }
-
- }
- extension IHLightBrightView{
-
- fileprivate func setViewColor(_ color :UIColor) {
-
- self.colors = [getAlphaColor(color).cgColor,color.cgColor]
- }
- fileprivate func reset() {
- isLock = false // 解锁
- self.isHorizontalMove = false
- self.isVerticalMove = false
- self.whitePickerView.isHidden = true
- self.brightnessPickerView.isHidden = true
- self.touchView.isHidden = true
- self.lampInfoBox.isHidden = true
- self.whiteThumb.isHidden = true
- self.brightThumb.isHidden = true
- Intermediate.isRegulateColor = "0"
- if isMove == true {
- if isVertical == true {
- if let brightStr = self.finalBrightness {
- if let block = self.brightnessCallback {
- block(brightStr)
- }
- }
- }else{
- if let colorStr = self.finalColorTemperature {
- if let block = self.colorTempCallback {
- block(colorStr)
- }
- }
- }
- }
- isMove = false
- }
-
-
- fileprivate func setColorBrightness(_ brightness:CGFloat){
- let bright = brightness * 0.8
- if let saturation = self.saturation {
- let color = UIColor(hue: hvalue, saturation: saturation, brightness: bright, alpha: 1)
- setViewColor(color)
-
- }
- }
-
- fileprivate func getHvalue(_ color :UIColor) ->CGFloat{
- var r : Float = 0
- var g : Float = 0
- var b : Float = 0
- var a : Float = 0
- var h : Float = 0
- var s : Float = 0
- var v : Float = 0
- let comp = color.cgColor.components
- r = Float(comp![0])
- g = Float(comp![1])
- b = Float(comp![2])
- a = Float(comp![3])
- RGBToHSV(r, g, b, &h, &s, &v, true)
- return CGFloat(h)
- }
-
-
- fileprivate func getAlphaColor(_ color:UIColor) -> UIColor {
- let coms = color.cgColor.components
- var r ,g,b : CGFloat
- //考虑灰度情况
- if coms?.count == 2 {
- r = CGFloat((coms?.first)! * CGFloat(255))
- g = CGFloat((coms?.first)! * CGFloat(255))
- b = CGFloat((coms?.first)! * CGFloat(255))
- }else{
- r = CGFloat((coms?[0])! * CGFloat(255))
- g = CGFloat((coms?[1])! * CGFloat(255))
- b = CGFloat((coms?[2])! * CGFloat(255))
- }
-
- return UIColor(red: r, green: g, blue: b, alpha: 0.12)
- }
-
- }
|