PQCustomSwitchView.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //
  2. // PQCustomSwitchView.swift
  3. // PQSpeed
  4. //
  5. // Created by SanW on 2021/5/12.
  6. // Copyright © 2021 BytesFlow. All rights reserved.
  7. //
  8. import UIKit
  9. import BFCommonKit
  10. class PQCustomSwitchView: UIView {
  11. /// 当前选中的item
  12. var currentItemBtn: UIButton?
  13. /// 按钮点击的回调
  14. var switchChangeHandle: ((_ sender: UIButton) -> Void)?
  15. var saveBtns:Array<UIButton> = Array.init()
  16. override private init(frame: CGRect) {
  17. super.init(frame: frame)
  18. }
  19. required init?(coder _: NSCoder) {
  20. fatalError("init(coder:) has not been implemented")
  21. }
  22. /*
  23. 服务器返回的 (1:快节奏,2:适中,3:慢节奏) ,
  24. 一,快慢速模式下取卡点 2 3 4
  25. 二,跳跃卡点模式下根据不同速度 取卡点 1,2,3
  26. 快节奏为选中区域的所有点位,即0,1,2,3,4 5 6 7 8 9 10 ……
  27. 适中为每两个点位取一个,即0,2,4,6……
  28. 慢节奏为每三个点位取一个,即0,3,6,9……
  29. */
  30. init(frame: CGRect, titles: [String], defaultIndex: Int = 1) {
  31. super.init(frame: frame)
  32. BFLog(message: "初始时选择的位置 is \(defaultIndex)")
  33. let itemWidth: CGFloat = frame.width / CGFloat(titles.count)
  34. for (index, itemTitle) in titles.enumerated() {
  35. let itemBtn = UIButton(type: .custom)
  36. //8 是每一个btn 的间隔
  37. itemBtn.frame = CGRect(x: CGFloat(index) * itemWidth + CGFloat(index) * 8, y: 0, width: itemWidth, height: frame.height)
  38. itemBtn.tag = (3 - index)
  39. itemBtn.setTitle(itemTitle, for: .normal)
  40. itemBtn.setTitleColor(UIColor.hexColor(hexadecimal: "#959595"), for: .normal)
  41. itemBtn.setTitleColor(UIColor.hexColor(hexadecimal: BFConfig.shared.styleColor.rawValue), for: .selected)
  42. itemBtn.addTarget(self, action: #selector(btnClick(sender:)), for: .touchUpInside)
  43. if itemBtn.tag == defaultIndex {
  44. itemBtn.isSelected = true
  45. currentItemBtn = itemBtn
  46. let styleColor = UIColor.hexColor(hexadecimal: BFConfig.shared.styleColor.rawValue)
  47. itemBtn.backgroundColor = UIColor(red: styleColor.rgbaf[0], green: styleColor.rgbaf[1], blue: styleColor.rgbaf[2], alpha: 0.15)
  48. itemBtn.titleLabel?.font = UIFont.boldSystemFont(ofSize: 13)
  49. }else{
  50. itemBtn.backgroundColor = BFConfig.shared.pointEditNamalBackgroundColor
  51. itemBtn.titleLabel?.font = UIFont.systemFont(ofSize: 13)
  52. }
  53. itemBtn.addCorner(corner:5)
  54. addSubview(itemBtn)
  55. saveBtns.append(itemBtn)
  56. }
  57. }
  58. func selectOneBtn(Index:Int) {
  59. let selectIndex = saveBtns.firstIndex(where: { (svaeBtn) -> Bool in
  60. (svaeBtn.tag == Index)
  61. })
  62. BFLog(message: "选择节奏 \(selectIndex ?? -1)")
  63. updateSelectBtn(sender: saveBtns[selectIndex ?? 0])
  64. }
  65. func updateSelectBtn(sender: UIButton) {
  66. currentItemBtn?.isSelected = false
  67. currentItemBtn?.titleLabel?.font = UIFont.systemFont(ofSize: 13)
  68. currentItemBtn?.backgroundColor = BFConfig.shared.pointEditNamalBackgroundColor
  69. sender.titleLabel?.font = UIFont.boldSystemFont(ofSize: 13)
  70. sender.isSelected = true
  71. let styleColor = UIColor.hexColor(hexadecimal: BFConfig.shared.styleColor.rawValue)
  72. sender.backgroundColor = UIColor(red: styleColor.rgbaf[0], green: styleColor.rgbaf[1], blue: styleColor.rgbaf[2], alpha: 0.15)
  73. currentItemBtn = sender
  74. }
  75. /// 按钮点击事件
  76. /// - Parameter sender: <#sender description#>
  77. /// - Returns: <#description#>
  78. @objc func btnClick(sender: UIButton) {
  79. updateSelectBtn(sender: sender)
  80. if switchChangeHandle != nil {
  81. switchChangeHandle!(sender)
  82. }
  83. }
  84. }