IHCurtainCell.swift 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. //
  2. // IHCurtainCell.swift
  3. // Inhealth
  4. //
  5. // Created by weclouds on 2020/7/25.
  6. // Copyright © 2020 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. let KNotificationCurtain = "KNotificationCurtain"
  10. class IHCurtainCell: UITableViewCell {
  11. private var labelTitle : UILabel?
  12. private var leftBrightnessBtn : UIButton?
  13. private var valueBackView : UIView?
  14. private var tableView : UITableView?
  15. var indexPath : IndexPath?
  16. private var choseBtnTag : Int = 0
  17. private var choseValue : String = ""
  18. let model = ["关","开"]
  19. var dataSource : [String: String]?{
  20. didSet{
  21. if let key = dataSource?.keys.first{
  22. labelTitle?.text = key
  23. }
  24. if let value = dataSource?.values.first {
  25. leftBrightnessBtn?.setTitle(value, for: .normal)
  26. }
  27. }
  28. }
  29. class func curtainCellWithTableViewAndIndexPath(tableView : UITableView,indexPath : IndexPath) ->IHCurtainCell {
  30. let curtainId = "curtainId"
  31. var cell = tableView.dequeueReusableCell(withIdentifier: curtainId)
  32. if cell == nil{
  33. cell = IHCurtainCell.init(style: .default, reuseIdentifier: curtainId, tableView: tableView, indexPath: indexPath)
  34. }
  35. return cell as! IHCurtainCell
  36. }
  37. init(style: UITableViewCell.CellStyle, reuseIdentifier: String?,tableView : UITableView,indexPath : IndexPath){
  38. super.init(style: style, reuseIdentifier: reuseIdentifier)
  39. self.tableView = tableView
  40. // self.indexPath = indexPath
  41. createUI()
  42. }
  43. required init?(coder: NSCoder) {
  44. fatalError("init(coder:) has not been implemented")
  45. }
  46. }
  47. extension IHCurtainCell {
  48. func createUI(){
  49. let backView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: KSCREENWIDTH, height: 120))
  50. backView.backgroundColor = .white
  51. //line
  52. let line = UIView.init(frame: CGRect.init(x: 0, y: 119, width: KSCREENWIDTH, height: 1))
  53. line.backgroundColor = UIColor.init(red: 246/255.0, green: 245/255.0, blue: 248/255.0, alpha: 1.0)
  54. backView.addSubview(line)
  55. //上
  56. let labelTitle = UILabel.init(frame: CGRect.init(x: 5, y: 0, width: KSCREENWIDTH, height: 40))
  57. labelTitle.backgroundColor = UIColor.init(red: 246/255.0, green: 245/255.0, blue: 248/255.0, alpha: 1.0)
  58. backView.addSubview(labelTitle)
  59. self.labelTitle = labelTitle
  60. let h : CGFloat = 50
  61. //下
  62. let choseView = UIView.init(frame: CGRect.init(x: 0, y: 40, width: KSCREENWIDTH, height: h))
  63. backView.addSubview(choseView)
  64. let leftBrightnessBtn = UIButton.init(frame: CGRect.init(x: 10, y: 0, width: KSCREENWIDTH - 10 * 2, height: h))
  65. leftBrightnessBtn.setTitle("100%", for: .normal)
  66. leftBrightnessBtn.setTitleColor(.black, for: .normal)
  67. leftBrightnessBtn.setImage(UIImage.init(named: "xialaIcon"), for: .normal)
  68. leftBrightnessBtn.titleLabel?.font = UIFont.systemFont(ofSize: 15.0)
  69. leftBrightnessBtn.titleEdgeInsets = UIEdgeInsets.init(top: 0, left: -20, bottom: 0, right: 0)
  70. leftBrightnessBtn.semanticContentAttribute = .forceRightToLeft
  71. leftBrightnessBtn.addTarget(self, action: #selector(choseBrightness), for: .touchUpInside)
  72. leftBrightnessBtn.tag = 1
  73. //leftView.addSubview(leftBrightnessBtn)
  74. choseView.addSubview(leftBrightnessBtn)
  75. self.leftBrightnessBtn = leftBrightnessBtn
  76. self.contentView.addSubview(backView)
  77. }
  78. }
  79. extension IHCurtainCell {
  80. @objc func choseBrightness(sender : UIButton){
  81. self.choseBtnTag = sender.tag
  82. if valueBackView != nil {
  83. valueBackView?.removeFromSuperview()
  84. }
  85. createChosePicker(sender: sender)
  86. }
  87. func createChosePicker(sender : UIButton) {
  88. let point = sender.convert(sender.frame.origin, to: self.tableView)
  89. var valueBackView = UIView.init(frame: CGRect.init(x: (KSCREENWIDTH - point.x * 2) * 0.4 , y: point.y + 45, width: 90, height: 80))
  90. //屏幕底下的显示
  91. if (KSCREENHEIGHT - point.y) < 80 {
  92. //左侧的
  93. valueBackView = UIView.init(frame: CGRect.init(x: (KSCREENWIDTH - point.x * 2) * 0.4, y: point.y - 35 - 80, width: 90, height: 80))
  94. }
  95. valueBackView.backgroundColor = .lightGray
  96. self.tableView?.addSubview(valueBackView)
  97. self.valueBackView = valueBackView
  98. let valueTableView = UITableView.init(frame: CGRect.init(x: 1, y: 1, width: 88, height: 78))
  99. valueTableView.dataSource = self
  100. valueTableView.delegate = self
  101. valueTableView.rowHeight = 40
  102. valueBackView.addSubview(valueTableView)
  103. }
  104. }
  105. extension IHCurtainCell : UITableViewDataSource,UITableViewDelegate{
  106. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  107. return self.model.count
  108. }
  109. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  110. let modelId = "modelId"
  111. var cell = tableView.dequeueReusableCell(withIdentifier: modelId)
  112. if cell == nil {
  113. cell = UITableViewCell.init(style: .default, reuseIdentifier: modelId)
  114. }
  115. cell?.textLabel?.text = self.model[indexPath.row]
  116. return cell!
  117. }
  118. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  119. if self.choseBtnTag == 1 {
  120. self.choseValue = model[indexPath.row]
  121. leftBrightnessBtn?.setTitle(choseValue, for: .normal)
  122. choseBtnTag = 0
  123. valueBackView?.removeFromSuperview()
  124. NotificationCenter.default.post(name: NSNotification.Name(rawValue: KNotificationCurtain), object: nil, userInfo: ["indexPath":self.indexPath!,"choseValue":choseValue])
  125. }
  126. }
  127. }