IHWhiltLightView.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. //
  2. // IHWhiltLightView.swift
  3. // Inhealth
  4. //
  5. // Created by weclouds on 2020/4/20.
  6. // Copyright © 2020 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. import PKHUD
  10. class IHWhiltLightView: UIView {
  11. var lampControlTrunoff:(()->Void)?
  12. var colorTempCallback:((String)->Void)?
  13. var brightnessCallback:((String)->Void)?
  14. fileprivate var gradientLayer :IHGradientLayer?
  15. @IBOutlet weak var brightnessBtn: UIButton!
  16. @IBOutlet weak var colorTempBtn: UIButton!
  17. @IBOutlet weak var touchView: UIImageView!
  18. @IBOutlet weak var brightThumb: UIImageView!
  19. // @IBOutlet weak var colorPickerView: UIView!
  20. @IBOutlet weak var brightnessPickerView: UIView!
  21. @IBOutlet weak var whitePickerView: UIView!
  22. @IBOutlet weak var whiteThumb: UIImageView!
  23. @IBOutlet weak var lampInfoBox: UIView!
  24. var isVertical :Bool! = true //判断是水平滑动还是垂直滑动
  25. var isMove :Bool! = false //判断手指是否移动了
  26. var lightMode:String?
  27. var model : String = "" //学校灯为1 : EL-DP03-1200-LY时,不支持调色调
  28. var isVerticalMove :Bool! = false // 纵轴移动
  29. {
  30. didSet{
  31. if self.isVerticalMove == true {
  32. self.whitePickerView.isHidden = true
  33. self.whiteThumb.isHidden = true
  34. self.brightnessPickerView.isHidden = false
  35. self.brightThumb.isHidden = false
  36. }else {
  37. self.whitePickerView.isHidden = true
  38. self.whiteThumb.isHidden = true
  39. self.brightnessPickerView.isHidden = true
  40. self.brightThumb.isHidden = true
  41. }
  42. }
  43. }
  44. var isHorizontalMove :Bool! = false //横轴移动
  45. {
  46. didSet{
  47. if self.isHorizontalMove == true {
  48. self.whitePickerView.isHidden = false
  49. self.whiteThumb.isHidden = false
  50. self.brightnessPickerView.isHidden = true
  51. self.brightThumb.isHidden = true
  52. }else {
  53. self.whitePickerView.isHidden = true
  54. self.whiteThumb.isHidden = true
  55. self.brightnessPickerView.isHidden = true
  56. self.brightThumb.isHidden = true
  57. }
  58. }
  59. }
  60. var isLock:Bool? = false //加一把锁 保证单独执行
  61. var horizontalSensitivity : CGFloat = 30 //水平灵敏度
  62. let sensitivity : CGFloat = 50 //灵敏度 移动的又有效距离 / sensitivity
  63. let whitePickerWidth = KSCREENWIDTH - 80
  64. let beginY :CGFloat? = (KSCREENHEIGHT - 400) / 2
  65. let endY :CGFloat? = (KSCREENHEIGHT - 400) / 2 + 400
  66. var beginPoint : CGPoint?
  67. var movePoint : CGPoint?
  68. var colors :[CGColor]?{
  69. didSet{
  70. // setNeedsDisplay()
  71. if let colors = self.colors {
  72. if colors.count == 2 {
  73. self.gradientLayer?.colors = colors
  74. }else{
  75. let firstColor = UIColor(red: 1, green: 0, blue: 0, alpha: 0.12).cgColor
  76. let secondColor = UIColor(red: 1, green: 0, blue: 0, alpha: 1).cgColor
  77. self.gradientLayer?.colors = [firstColor,secondColor]
  78. }
  79. }
  80. }
  81. }
  82. var colorTemperature : Int?{
  83. didSet{
  84. }
  85. }
  86. var finalBrightness :String?
  87. var finalColorTemperature:String?
  88. var currentBrightness:String?{
  89. didSet{
  90. if let currentBrightness = self.currentBrightness {
  91. let bri: Int! = Int(currentBrightness)
  92. self.bright = endY! - CGFloat(bri) / 100 * 400
  93. // finalBrightness = currentBrightness
  94. }
  95. }
  96. }
  97. var currentColorTemp : String?{
  98. didSet{
  99. if let currentColorTemp = self.currentColorTemp {
  100. let colorTemp : Int! = Int(currentColorTemp)
  101. self.tvalue = (CGFloat(colorTemp) - 3000) / 3000 * whitePickerWidth + 40
  102. // finalColorTemperature = currentColorTemp
  103. }
  104. }
  105. }
  106. var saturation :CGFloat?
  107. var tvalue:CGFloat? {//色温的横坐标
  108. didSet{
  109. if let tvalue = self.tvalue {
  110. self.whiteThumb.center.x = tvalue
  111. self.colorTemperature = Int((tvalue - 40) / whitePickerWidth * 3000) + 3000
  112. self.colorTempBtn.setTitle("\(colorTemperature ?? 3000)k", for: .disabled)
  113. self.finalColorTemperature = "\(colorTemperature ?? 3000)"
  114. saturation = (tvalue - 40) / whitePickerWidth
  115. let aSaturation = saturation! * 0.7 + 0.3
  116. let aBrigghtness = (self.brightness ?? 1) * 0.8
  117. let color = UIColor(hue: hvalue, saturation: aSaturation , brightness: aBrigghtness, alpha: 1)
  118. setViewColor(color)
  119. }
  120. }
  121. }
  122. //颜色亮度
  123. var brightness :CGFloat?
  124. var bright : CGFloat?{
  125. didSet{
  126. if let bright = self.bright {
  127. self.brightThumb.center.y = (bright )
  128. let result = (400 - (bright - beginY!) ) / 400 * 100
  129. self.brightnessBtn.setTitle("\(Int(result))%", for: .disabled)
  130. self.finalBrightness = "\(Int(result))"
  131. if result == 0 {
  132. if let block = self.lampControlTrunoff {
  133. block()
  134. }
  135. }
  136. self.brightness = result / 100
  137. setColorBrightness(result / 100)
  138. }
  139. }
  140. }
  141. var hvalue :CGFloat!{
  142. return getHvalue(UIColor(hexString: "#FCD149"))
  143. }
  144. override func awakeFromNib() {
  145. super.awakeFromNib()
  146. gradientLayer = IHGradientLayer()
  147. gradientLayer?.frame = self.bounds
  148. self.layer.insertSublayer(gradientLayer!, at: 0)
  149. // beginY = self.brightnessPickerView.frame.minY
  150. // endY = self.brightnessPickerView.frame.maxY
  151. setViewColor(UIColor(hexString: "#FCD149"))
  152. setupUI()
  153. }
  154. func setupUI() {
  155. brightnessBtn.layoutButton(style: .Left, imageTitleSpace: 10)
  156. colorTempBtn.layoutButton(style: .Left, imageTitleSpace: 10)
  157. let bgLayer1 = CAGradientLayer()
  158. bgLayer1.colors = [UIColor.white.cgColor, UIColor(red: 236/255, green: 168/255, blue: 98/255, alpha: 1).cgColor]
  159. bgLayer1.locations = [0, 1]
  160. bgLayer1.frame = whitePickerView.bounds
  161. bgLayer1.startPoint = CGPoint(x: 0, y: 0)
  162. bgLayer1.endPoint = CGPoint(x: 1, y: 0)
  163. whitePickerView.layer.addSublayer(bgLayer1)
  164. self.brightThumb.center.y = self.brightnessPickerView.frame.minY
  165. self.bright = self.brightThumb.center.y
  166. self.whiteThumb.center.x = self.whitePickerView.frame.minX
  167. self.tvalue = self.whiteThumb.center.x
  168. }
  169. // override func draw(_ rect: CGRect) {
  170. // let colors = [UIColor.white.cgColor,UIColor.blue.cgColor]
  171. // if let cgColor = self.colors {
  172. // let endRadius = sqrt(pow(frame.width/2, 2) + pow(frame.height/2, 2))
  173. // let center = CGPoint(x: bounds.size.width / 2, y: bounds.size.height / 2)
  174. // let gradient = CGGradient(colorsSpace: nil, colors: cgColor as! CFArray, locations: nil)
  175. // let context = UIGraphicsGetCurrentContext()
  176. //
  177. // context?.drawRadialGradient(gradient!, startCenter: center, startRadius: 0.0, endCenter: center, endRadius: endRadius, options: CGGradientDrawingOptions.drawsBeforeStartLocation)
  178. // }
  179. // }
  180. }
  181. extension IHWhiltLightView{
  182. override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
  183. let touch : UITouch! = touches.first
  184. self.beginPoint = touch.location(in: self)
  185. self.touchView.isHidden = false
  186. self.touchView.center = self.beginPoint!
  187. }
  188. override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
  189. reset()
  190. }
  191. override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
  192. reset()
  193. }
  194. override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
  195. let touch : UITouch! = touches.first
  196. self.movePoint = touch.location(in: self)
  197. let previousPoint = touch.previousLocation(in: self)
  198. let directX = movePoint!.x - previousPoint.x
  199. let directY = movePoint!.y - previousPoint.y
  200. self.touchView.isHidden = false
  201. self.lampInfoBox.isHidden = false
  202. if self.lightMode == "0" {
  203. self.colorTempBtn.isHidden = true
  204. }
  205. self.touchView.center = self.movePoint!
  206. let height = movePoint!.y - beginPoint!.y
  207. let width = beginPoint!.x - movePoint!.x
  208. let rads = atan(height/width);
  209. let degrees = 180.0 * Double(rads) / .pi
  210. let deltaDegrees = abs(degrees) //角度的d绝对值
  211. log.debug(deltaDegrees)
  212. if deltaDegrees.isNaN == false {//结果为nan的时候 不计算
  213. if deltaDegrees < 45 {
  214. if isLock == false { //如果是开锁 则可以进来判断是否可以移动 ,
  215. if self.model == "1" {
  216. //学校灯
  217. HUD.flash(.label("该型号不支持调色温"), delay: 1)
  218. return;
  219. }
  220. isLock = true //进来之后就上锁
  221. isHorizontalMove = true
  222. isVerticalMove = false
  223. }
  224. }else{
  225. if isLock == false {
  226. isLock = true
  227. isHorizontalMove = false
  228. isVerticalMove = true
  229. }
  230. }
  231. }
  232. if isHorizontalMove == true{ // 水平滑动
  233. isMove = true
  234. isVertical = false
  235. self.brightThumb.isHidden = true
  236. self.brightnessBtn.isHidden = true
  237. self.brightnessPickerView.isHidden = true
  238. if self.lightMode != "0" {
  239. self.whitePickerView.isHidden = false
  240. self.whiteThumb.isHidden = false
  241. self.colorTempBtn.isHidden = false
  242. tvalue! += (directX )
  243. if tvalue! < 40 {
  244. tvalue = self.whitePickerView.frame.maxX //循环话就跳到尾部
  245. //tvalue = 40 //不循环就就恢复起点
  246. }
  247. if tvalue! > (self.whitePickerView.frame.maxX) {
  248. tvalue = 40
  249. // tvalue = self.whitePickerView.frame.maxX
  250. }
  251. }
  252. }
  253. if isVerticalMove == true { //垂直移动
  254. isMove = true
  255. isVertical = true
  256. self.whitePickerView.isHidden = true
  257. self.whiteThumb.isHidden = true
  258. self.colorTempBtn.isHidden = true
  259. self.brightnessBtn.isHidden = false
  260. self.brightnessPickerView.isHidden = false
  261. self.brightThumb.isHidden = false
  262. bright! += (directY ) //弹性系数
  263. if bright! < beginY!{
  264. bright = beginY
  265. }
  266. if bright! > endY!{
  267. bright = endY
  268. }
  269. }
  270. }
  271. }
  272. extension IHWhiltLightView{
  273. fileprivate func setViewColor(_ color :UIColor) {
  274. self.colors = [getAlphaColor(color).cgColor,color.cgColor]
  275. }
  276. fileprivate func reset() {
  277. isLock = false // 解锁
  278. self.isHorizontalMove = false
  279. self.isVerticalMove = false
  280. self.whitePickerView.isHidden = true
  281. self.brightnessPickerView.isHidden = true
  282. self.touchView.isHidden = true
  283. self.lampInfoBox.isHidden = true
  284. self.whiteThumb.isHidden = true
  285. self.brightThumb.isHidden = true
  286. Intermediate.isRegulateColor = "0"
  287. if isMove == true {
  288. if isVertical == true {
  289. if let brightStr = self.finalBrightness {
  290. if let block = self.brightnessCallback {
  291. block(brightStr)
  292. }
  293. }
  294. }else{
  295. if let colorStr = self.finalColorTemperature {
  296. if let block = self.colorTempCallback {
  297. block(colorStr)
  298. }
  299. }
  300. }
  301. }
  302. isMove = false
  303. }
  304. fileprivate func setColorBrightness(_ brightness:CGFloat){
  305. let bright = brightness * 0.8
  306. if let saturation = self.saturation {
  307. let color = UIColor(hue: hvalue, saturation: saturation, brightness: bright, alpha: 1)
  308. setViewColor(color)
  309. }
  310. }
  311. fileprivate func getHvalue(_ color :UIColor) ->CGFloat{
  312. var r : Float = 0
  313. var g : Float = 0
  314. var b : Float = 0
  315. var a : Float = 0
  316. var h : Float = 0
  317. var s : Float = 0
  318. var v : Float = 0
  319. let comp = color.cgColor.components
  320. r = Float(comp![0])
  321. g = Float(comp![1])
  322. b = Float(comp![2])
  323. a = Float(comp![3])
  324. RGBToHSV(r, g, b, &h, &s, &v, true)
  325. return CGFloat(h)
  326. }
  327. fileprivate func getAlphaColor(_ color:UIColor) -> UIColor {
  328. let coms = color.cgColor.components
  329. var r ,g,b : CGFloat
  330. //考虑灰度情况
  331. if coms?.count == 2 {
  332. r = CGFloat((coms?.first)! * CGFloat(255))
  333. g = CGFloat((coms?.first)! * CGFloat(255))
  334. b = CGFloat((coms?.first)! * CGFloat(255))
  335. }else{
  336. r = CGFloat((coms?[0])! * CGFloat(255))
  337. g = CGFloat((coms?[1])! * CGFloat(255))
  338. b = CGFloat((coms?[2])! * CGFloat(255))
  339. }
  340. return UIColor(red: r, green: g, blue: b, alpha: 0.12)
  341. }
  342. }