O2StringPicker.swift 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //
  2. // O2StringPicker.swift
  3. // O2Platform
  4. //
  5. // Created by FancyLou on 2020/6/30.
  6. // Copyright © 2020 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. class O2StringPicker: UIView, NibLoadable {
  10. @IBOutlet weak var titleLabel: UILabel!
  11. @IBOutlet weak var pickerView: UIPickerView!
  12. @IBOutlet weak var mainViewBottomLayout: NSLayoutConstraint!
  13. @IBOutlet weak var mainView: UIView!
  14. @IBAction func cancelBtnAction(_ sender: UIButton) {
  15. self.dismiss()
  16. }
  17. @IBAction func okBtnAction(_ sender: UIButton) {
  18. completionHandler(self.selectItemValue ?? "")
  19. dismiss()
  20. }
  21. private var completionHandler: (_ result: String) -> Void = {_ in }
  22. private var itemArray: [String] = []
  23. private var valueArray: [String] = []
  24. private var selectItemValue: String? = nil
  25. class func instance(items: [String],completionHandler: @escaping (_ result: String) -> Void, values:[String]? = nil, pickerTitle:String? = nil, currentValue: String? = nil) -> O2StringPicker{
  26. let view: O2StringPicker = O2StringPicker.loadViewFromNib()
  27. view.completionHandler = completionHandler
  28. view.itemArray = items
  29. if let v = values, v.count > 0 {
  30. view.valueArray = v
  31. }
  32. view.selectItemValue = currentValue
  33. view.titleLabel.text = pickerTitle ?? ""
  34. view.setupUI()
  35. return view
  36. }
  37. private func setupUI() {
  38. self.mainViewBottomLayout.constant = 320
  39. self.backgroundColor = UIColor.hexRGB(0x000000, 0.0)
  40. let tap = UITapGestureRecognizer(target: self, action: #selector(dismiss))
  41. tap.delegate = self
  42. addGestureRecognizer(tap)
  43. pickerView.delegate = self
  44. pickerView.dataSource = self
  45. }
  46. func show() {
  47. UIApplication.shared.keyWindow!.addSubview(self)
  48. self.frame = UIScreen.main.bounds
  49. UIView.animate(withDuration: 0.3, animations: {
  50. self.mainViewBottomLayout.constant = 0
  51. self.backgroundColor = UIColor.hexRGB(0x000000, 0.5)
  52. self.layoutIfNeeded()
  53. }, completion: { (finish) in
  54. //选中
  55. var row = 0
  56. if let item = self.selectItemValue {
  57. if self.valueArray.count > 0 {
  58. self.valueArray.forEachEnumerated { (index, value) in
  59. if item == value {
  60. row = index
  61. }
  62. }
  63. }else {
  64. self.itemArray.forEachEnumerated { (index, value) in
  65. if item == value {
  66. row = index
  67. }
  68. }
  69. }
  70. }
  71. self.pickerView.selectRow(row, inComponent: 0, animated: true)
  72. })
  73. }
  74. @objc func dismiss() {
  75. UIView.animate(withDuration: 0.3, animations: {
  76. self.mainViewBottomLayout.constant = 320
  77. self.backgroundColor = UIColor.hexRGB(0x000000, 0.0)
  78. self.layoutIfNeeded()
  79. }, completion: { (finished) in
  80. self.removeFromSuperview()
  81. })
  82. }
  83. }
  84. extension O2StringPicker: UIPickerViewDelegate, UIPickerViewDataSource {
  85. func numberOfComponents(in pickerView: UIPickerView) -> Int {
  86. //仅支持一组 多组联动未实现
  87. return 1
  88. }
  89. func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
  90. return self.itemArray.count
  91. }
  92. func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
  93. return 40
  94. }
  95. func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
  96. let title = self.itemArray[row]
  97. if let label = view as? UILabel {
  98. label.text = title
  99. return label
  100. }
  101. let label = UILabel()
  102. label.textAlignment = .center
  103. label.font = UIFont.systemFont(ofSize: 17)
  104. label.text = title
  105. label.sizeToFit()
  106. return label
  107. }
  108. func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
  109. self.selectItemValue = self.valueArray[row]
  110. }
  111. }
  112. extension O2StringPicker: UIGestureRecognizerDelegate {
  113. func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
  114. guard let touchView = touch.view else { return false }
  115. if touchView.isDescendant(of: mainView) {
  116. // 点击的view是否是mainView或者mainView的子视图
  117. return false
  118. }
  119. return true
  120. }
  121. }