BFStripSwithView.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // BFStripSwithView.swift
  3. // BFRecordScreenKit
  4. //
  5. // Created by SanW on 2021/12/15.
  6. // Copyright © 2021 BytesFlow. All rights reserved.
  7. //
  8. import BFCommonKit
  9. import UIKit
  10. open class BFStripSwithView: UIView {
  11. public var itemClickHandle: ((_ sender: UIButton, _ index: Int) -> Void)?
  12. var currentIndex: Int = 0
  13. var itemSpace: CGFloat = 4
  14. var itemHeight: CGFloat = 6
  15. var itemNormalColor: UIColor = UIColor.white
  16. var itemSelectedColor: UIColor = UIColor.hexColor(hexadecimal: "#389AFF")
  17. var strips: Int = 2
  18. var currentItem: UIButton?
  19. lazy var selectedBtn: UIButton = {
  20. let selectedBtn = UIButton(type: .custom)
  21. return selectedBtn
  22. }()
  23. override init(frame: CGRect) {
  24. super.init(frame: frame)
  25. }
  26. public required init?(coder _: NSCoder) {
  27. fatalError("init(coder:) has not been implemented")
  28. }
  29. public init(frame: CGRect, items: Int, index: Int? = nil, normalColor: UIColor? = nil, selectedColor: UIColor? = nil, space: CGFloat = 10) {
  30. super.init(frame: frame)
  31. strips = items
  32. if normalColor != nil {
  33. itemNormalColor = normalColor!
  34. }
  35. if selectedColor != nil {
  36. itemSelectedColor = selectedColor!
  37. }
  38. if index != nil, (index ?? 0) < items {
  39. currentIndex = index!
  40. }
  41. itemSpace = space
  42. selectedBtn.addCorner(corner: itemHeight / 2)
  43. selectedBtn.backgroundColor = itemSelectedColor
  44. }
  45. override public func layoutSubviews() {
  46. super.layoutSubviews()
  47. addSubviews()
  48. }
  49. private func addSubviews() {
  50. if currentItem != nil {
  51. return
  52. }
  53. subviews.forEach { view in
  54. view.removeFromSuperview()
  55. }
  56. if strips > 0 {
  57. let margin: CGFloat = (frame.width - CGFloat(strips - 1) * itemSpace) / CGFloat(strips)
  58. for index in 0 ..< strips {
  59. let itemBtn = UIButton(type: .custom)
  60. itemBtn.frame = CGRect(x: CGFloat(index) * (itemSpace + margin), y: (frame.height - itemHeight) / 2, width: margin, height: itemHeight)
  61. itemBtn.tag = index + 1
  62. // itemBtn.addTarget(self, action: #selector(changeSwitch(sender:)), for: .touchUpInside)
  63. itemBtn.addCorner(corner: itemHeight / 2)
  64. itemBtn.backgroundColor = itemNormalColor
  65. if itemBtn.tag == currentIndex + 1 {
  66. currentItem = itemBtn
  67. selectedBtn.frame = itemBtn.frame
  68. }
  69. addSubview(itemBtn)
  70. }
  71. if selectedBtn.superview == nil {
  72. addSubview(selectedBtn)
  73. }
  74. bringSubviewToFront(selectedBtn)
  75. }
  76. }
  77. @objc public func changeSwitch(sender: UIButton) {
  78. if sender == currentItem {
  79. return
  80. }
  81. if itemClickHandle != nil {
  82. itemClickHandle!(sender, Int(sender.tag - 1))
  83. }
  84. }
  85. public func changeSwitchStatus(index: Int) {
  86. let sender: UIButton? = viewWithTag(Int(index) + 1) as? UIButton
  87. if sender == nil || sender == currentItem {
  88. return
  89. }
  90. currentItem = sender
  91. UIView.animate(withDuration: 0.1) { [weak self] in
  92. self?.selectedBtn.center.x = CGFloat(sender!.center.x)
  93. }
  94. }
  95. }