IHRoomHistoryToolBar.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //
  2. // IHRoomHistoryToolBar.swift
  3. // Inhealth
  4. //
  5. // Created by weclouds on 2019/12/17.
  6. // Copyright © 2019 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. protocol IHRoomHistoryToolBarDelegate : NSObjectProtocol {
  10. func roomHistorydidSelectedLeftBarItem(at index:Int)
  11. func roomHistorydidSelectedRightBarItem(at index:Int)
  12. }
  13. class IHRoomHistoryToolBar: UIView {
  14. var currentDateType:Int? = 0{
  15. didSet{
  16. if let dateType = self.currentDateType {
  17. for i in 0..<titles.count {
  18. let btn = viewWithTag(1400 + i) as! UIButton
  19. if i == dateType {
  20. btn.isSelected = true
  21. // #05CFAB
  22. btn.backgroundColor = UIColor(hexString: "#573F95")
  23. }else{
  24. btn.isSelected = false
  25. btn.backgroundColor = UIColor(hexString: "#F6F8F7")
  26. }
  27. }
  28. }
  29. }
  30. }
  31. var devType:String? = "1"{
  32. didSet{
  33. if let devType = self.devType {
  34. if devType == "1" || devType == "2" || devType == "4" {
  35. titles = ["当天","本周","当月","当年","自定义"]
  36. }else{
  37. titles = ["当天","本周","当月","当年"]
  38. }
  39. configRightBar()
  40. }
  41. }
  42. }
  43. var titles : [String] = ["当天","本周","当月","当年"]
  44. weak var delegate: IHRoomHistoryToolBarDelegate?
  45. override init(frame: CGRect) {
  46. super.init(frame: frame)
  47. configLeftBar()
  48. }
  49. required init?(coder: NSCoder) {
  50. fatalError("init(coder:) has not been implemented")
  51. }
  52. func configLeftBar() {
  53. for i in 0..<2 {
  54. let button = UIButton(type: .custom)
  55. button.frame = CGRect(x:Double( 20 + i * 35), y: 14.5, width: 35, height: 23)
  56. button.tag = 1300 + i
  57. button.backgroundColor = UIColor(hexString: "#F6F8F7")
  58. button.layer.cornerRadius = 3
  59. button.layer.masksToBounds = true
  60. if i == 0 {
  61. button.setImage(UIImage(named: "折线图_未选中"), for: .normal)
  62. button.setImage(UIImage(named: "折线图_选中"), for: .normal)
  63. }else {
  64. button.setImage(UIImage(named: "柱状图_未选中"), for: .normal)
  65. button.setImage(UIImage(named: "柱状图_选中"), for: .normal)
  66. }
  67. if i == self.currentDateType {
  68. button.isSelected = true
  69. button.backgroundColor = UIColor(hexString: "#573F95")
  70. }
  71. button.addTarget(self, action: #selector(selectedLeftAction), for: .touchUpInside)
  72. addSubview(button)
  73. }
  74. }
  75. func configRightBar() {
  76. let width = (KSCREENWIDTH - 110 - 36 ) / CGFloat(titles.count)
  77. for i in 0..<titles.count {
  78. let button = UIButton(type: .custom)
  79. button.frame = CGRect(x: 110 + CGFloat(i) * width, y: 14.5, width: width + 1, height: 23)
  80. button.tag = 1400 + i
  81. button.backgroundColor = UIColor(hexString: "#F6F8F7")
  82. button.layer.cornerRadius = 3
  83. button.layer.masksToBounds = true
  84. button.setTitle(titles[i], for: .normal)
  85. button.titleLabel?.font = UIFont(name: Alibaba_PuHuiTi_Regular, size: 12)
  86. button.setTitleColor(UIColor(hexString: "#657085"), for: .normal)
  87. button.setTitleColor(UIColor(hexString: "#FFFFFF"), for: .selected)
  88. if i == 0 {
  89. button.isSelected = true
  90. button.backgroundColor = UIColor(hexString: "#573F95")
  91. button.titleLabel?.font = UIFont(name: Alibaba_PuHuiTi_Medium, size: 12)
  92. }
  93. button.addTarget(self, action: #selector(selectedRightAction), for: .touchUpInside)
  94. addSubview(button)
  95. }
  96. }
  97. @objc func selectedLeftAction(_ sender:UIButton) {
  98. for i in 0..<2 {
  99. let btn = viewWithTag(1300 + i) as! UIButton
  100. btn.isSelected = false
  101. btn.backgroundColor = UIColor(hexString: "#F6F8F7")
  102. }
  103. sender.isSelected = true
  104. sender.backgroundColor = UIColor(hexString: "#573F95")
  105. if let delegate = delegate {
  106. delegate.roomHistorydidSelectedLeftBarItem(at: sender.tag - 1300)
  107. }
  108. }
  109. @objc func selectedRightAction(_ sender:UIButton) {
  110. for i in 0..<titles.count {
  111. let btn = viewWithTag(1400 + i) as! UIButton
  112. btn.isSelected = false
  113. //#573F95 "#F6F8F7
  114. //
  115. btn.backgroundColor = UIColor(hexString: "#F6F8F7")
  116. btn.titleLabel?.font = UIFont(name: Alibaba_PuHuiTi_Regular, size: 12)
  117. }
  118. sender.isSelected = true
  119. sender.backgroundColor = UIColor(hexString: "#573F95")
  120. sender.titleLabel?.font = UIFont(name: Alibaba_PuHuiTi_Medium, size: 12)
  121. if let delegate = delegate {
  122. delegate.roomHistorydidSelectedRightBarItem(at: sender.tag - 1400)
  123. }
  124. }
  125. }