DropBoxView.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //
  2. // DropBoxView.swift
  3. // Bark
  4. //
  5. // Created by huangfeng on 2023/2/2.
  6. // Copyright © 2023 Fin. All rights reserved.
  7. //
  8. import DropDown
  9. import UIKit
  10. import RxCocoa
  11. import RxSwift
  12. class DropBoxView: UIView {
  13. let valueLabel: UILabel = {
  14. let label = UILabel()
  15. label.font = UIFont.systemFont(ofSize: 14)
  16. label.textColor = BKColor.grey.darken3
  17. return label
  18. }()
  19. let dropIconView: UIImageView = {
  20. let imageView = UIImageView()
  21. imageView.image = UIImage(named: "baseline_keyboard_arrow_down_black_24pt")?.withRenderingMode(.alwaysTemplate)
  22. imageView.tintColor = BKColor.grey.lighten2
  23. return imageView
  24. }()
  25. var isSelecting: Bool = true {
  26. didSet {
  27. UIView.animate(withDuration: 0.3) {
  28. if self.isSelecting {
  29. self.borderColor = BKColor.blue.darken5
  30. self.shadowColor = BKColor.blue.darken5
  31. self.layer.shadowOpacity = 0.3
  32. }
  33. else {
  34. self.borderColor = BKColor.grey.lighten2
  35. self.shadowColor = BKColor.grey.lighten2
  36. self.layer.shadowOpacity = 0
  37. }
  38. }
  39. }
  40. }
  41. var values: [String] {
  42. didSet {
  43. self.currentValue = values.first
  44. }
  45. }
  46. var currentValue: String? {
  47. didSet {
  48. self.valueLabel.text = currentValue
  49. self.currentValueChanged?(self.currentValue)
  50. }
  51. }
  52. var currentValueChanged: ((String?) -> Void)?
  53. init(values: [String]) {
  54. self.values = values
  55. super.init(frame: CGRect.zero)
  56. self.backgroundColor = BKColor.white
  57. self.borderColor = BKColor.grey.lighten2
  58. self.borderWidthPreset = .border2
  59. self.cornerRadiusPreset = .cornerRadius3
  60. self.shadowColor = BKColor.grey.lighten2
  61. self.layer.shadowOffset = CGSize(width: 0, height: 0)
  62. self.layer.shadowRadius = 2
  63. self.layer.shadowOpacity = 0
  64. addSubview(valueLabel)
  65. addSubview(dropIconView)
  66. self.dropIconView.snp.makeConstraints { make in
  67. make.centerY.equalToSuperview()
  68. make.right.equalTo(-6)
  69. }
  70. self.valueLabel.snp.makeConstraints { make in
  71. make.centerY.equalToSuperview()
  72. make.left.equalTo(16)
  73. }
  74. self.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(tap)))
  75. defer {
  76. self.currentValue = self.values.first
  77. }
  78. }
  79. @available(*, unavailable)
  80. required init?(coder: NSCoder) {
  81. fatalError("init(coder:) has not been implemented")
  82. }
  83. @objc func tap() {
  84. let dropDown = DropDown(anchorView: self)
  85. dropDown.cellNib = UINib(nibName: "BKDropDownCell", bundle: Bundle(for: BKDropDownCell.self))
  86. dropDown.cellHeight = 50
  87. dropDown.cornerRadius = 10
  88. dropDown.clipsToBounds = true
  89. dropDown.bottomOffset = CGPoint(x: 0, y: 50)
  90. dropDown.dataSource = self.values
  91. dropDown.selectionAction = { [weak self] _, str in
  92. self?.currentValue = str
  93. self?.isSelecting = false
  94. }
  95. dropDown.cancelAction = { [weak self] in
  96. self?.isSelecting = false
  97. }
  98. self.isSelecting = true
  99. dropDown.show()
  100. }
  101. }
  102. extension Reactive where Base: DropBoxView {
  103. var currentValueChanged: ControlEvent<String?> {
  104. let source = Observable<String?>.create { [weak control = self.base] observer -> Disposable in
  105. MainScheduler.ensureExecutingOnScheduler()
  106. guard let control = control else {
  107. observer.onCompleted()
  108. return Disposables.create()
  109. }
  110. control.currentValueChanged = { value in
  111. observer.onNext(value)
  112. }
  113. return Disposables.create()
  114. }
  115. return ControlEvent(events: source)
  116. }
  117. }