|
| 1 | +// |
| 2 | +// OptionPickerControl.swift |
| 3 | +// Flashcards |
| 4 | +// |
| 5 | +// Created by Ben on 27/11/2017. |
| 6 | +// Copyright © 2017 bcylin. |
| 7 | +// |
| 8 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | +// of this software and associated documentation files (the "Software"), to deal |
| 10 | +// in the Software without restriction, including without limitation the rights |
| 11 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | +// copies of the Software, and to permit persons to whom the Software is |
| 13 | +// furnished to do so, subject to the following conditions: |
| 14 | +// |
| 15 | +// The above copyright notice and this permission notice shall be included in all |
| 16 | +// copies or substantial portions of the Software. |
| 17 | +// |
| 18 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 | +// SOFTWARE. |
| 25 | +// |
| 26 | + |
| 27 | +import UIKit |
| 28 | + |
| 29 | +open class OptionPickerControl<T: OptionDescriptive>: UIControl, |
| 30 | + UIPickerViewDataSource, |
| 31 | + UIPickerViewDelegate { |
| 32 | + |
| 33 | + // MARK: - Initialization |
| 34 | + |
| 35 | + public init() { |
| 36 | + super.init(frame: .zero) |
| 37 | + addSubview(hiddenTextField) |
| 38 | + } |
| 39 | + |
| 40 | + required public init?(coder aDecoder: NSCoder) { |
| 41 | + fatalError("init(coder:) is not supported") |
| 42 | + } |
| 43 | + |
| 44 | + // MARK: - Properties |
| 45 | + |
| 46 | + public var options: [Option<T>] = [Option<T>.optional()] |
| 47 | + public var selectedOption: Option<T> = Option<T>.optional() { |
| 48 | + didSet { |
| 49 | + if hiddenTextField.isFirstResponder { |
| 50 | + sendActions(for: .valueChanged) |
| 51 | + } else if let index = options.index(of: selectedOption) { |
| 52 | + picker.selectRow(index, inComponent: 0, animated: false) |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + // MARK: - Lazy Instantiation |
| 58 | + |
| 59 | + private lazy var doneBarButton: UIBarButtonItem = |
| 60 | + UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(dismissPicker(_:))) |
| 61 | + |
| 62 | + private lazy var pickerToolbar: UIToolbar = { |
| 63 | + let toolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 44)) |
| 64 | + toolbar.items = [ |
| 65 | + UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil), |
| 66 | + self.doneBarButton |
| 67 | + ] |
| 68 | + return toolbar |
| 69 | + }() |
| 70 | + |
| 71 | + private lazy var picker: UIPickerView = { |
| 72 | + let picker = UIPickerView() |
| 73 | + picker.dataSource = self |
| 74 | + picker.delegate = self |
| 75 | + return picker |
| 76 | + }() |
| 77 | + |
| 78 | + private lazy var hiddenTextField: UITextField = { |
| 79 | + let textField = UITextField() |
| 80 | + textField.inputAccessoryView = self.pickerToolbar |
| 81 | + textField.inputView = self.picker |
| 82 | + return textField |
| 83 | + }() |
| 84 | + |
| 85 | + // MARK: - UIResponder |
| 86 | + |
| 87 | + @discardableResult |
| 88 | + override open func becomeFirstResponder() -> Bool { |
| 89 | + return super.becomeFirstResponder() || hiddenTextField.becomeFirstResponder() |
| 90 | + } |
| 91 | + |
| 92 | + @discardableResult |
| 93 | + override open func resignFirstResponder() -> Bool { |
| 94 | + return hiddenTextField.resignFirstResponder() |
| 95 | + } |
| 96 | + |
| 97 | + // MARK: - UIPickerViewDataSource |
| 98 | + |
| 99 | + open func numberOfComponents(in pickerView: UIPickerView) -> Int { |
| 100 | + return 1 |
| 101 | + } |
| 102 | + |
| 103 | + open func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { |
| 104 | + return options.count |
| 105 | + } |
| 106 | + |
| 107 | + // MARK: - UIPickerViewDelegate |
| 108 | + |
| 109 | + open func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { |
| 110 | + return options[row].title |
| 111 | + } |
| 112 | + |
| 113 | + open func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { |
| 114 | + selectedOption = options[row] |
| 115 | + } |
| 116 | + |
| 117 | + // MARK: - IBActions |
| 118 | + |
| 119 | + @objc private func dismissPicker(_ sender: Any) { |
| 120 | + resignFirstResponder() |
| 121 | + } |
| 122 | + |
| 123 | +} |
0 commit comments