|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// UIPickerView.swift |
| 4 | +// |
| 5 | +// Copyright (c) 2016 Richard Piazza |
| 6 | +// https://github.com/richardpiazza/CodeQuickKit |
| 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 | + |
| 28 | +import UIKit |
| 29 | + |
| 30 | +extension UIPickerView { |
| 31 | + |
| 32 | + private class Manager: PickerViewControllerDelegate { |
| 33 | + var pickerViewController: PickerViewController = PickerViewController() |
| 34 | + var presentingView: UIView? |
| 35 | + |
| 36 | + func offScreenFrame() -> CGRect { |
| 37 | + if let view = presentingView { |
| 38 | + return CGRect(x: view.bounds.origin.x, y: view.bounds.size.height + PickerViewController.defaultViewHeight, width: view.bounds.size.width, height: PickerViewController.defaultViewHeight) |
| 39 | + } else { |
| 40 | + return CGRect(x: 0.0, y: -PickerViewController.defaultViewHeight, width: PickerViewController.defaultViewWidth, height: PickerViewController.defaultViewHeight) |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + func onScreenFrame() -> CGRect { |
| 45 | + if let view = presentingView { |
| 46 | + return CGRect(x: view.bounds.origin.x, y: view.bounds.size.height - PickerViewController.defaultViewHeight, width: view.bounds.size.width, height: PickerViewController.defaultViewHeight) |
| 47 | + } else { |
| 48 | + return CGRect(x: 0.0, y: 0.0, width: PickerViewController.defaultViewWidth, height: PickerViewController.defaultViewHeight) |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + func present(fromView: UIView, withTitle title: String?, configuration: UIPickerViewConfigurationBlock) { |
| 53 | + presentingView = fromView |
| 54 | + pickerViewController.toolbarTitle.title = title ?? "" |
| 55 | + configuration(pickerView: pickerViewController.picker) |
| 56 | + |
| 57 | + pickerViewController.view.frame = offScreenFrame() |
| 58 | + fromView.addSubview(pickerViewController.view) |
| 59 | + UIView.animateWithDuration(0.5) { |
| 60 | + self.pickerViewController.view.frame = self.onScreenFrame() |
| 61 | + } |
| 62 | + |
| 63 | + NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(Manager.uiKeyboardWillShow(_:)), name: UIKeyboardWillShowNotification, object: nil) |
| 64 | + } |
| 65 | + |
| 66 | + func resign() { |
| 67 | + NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: nil) |
| 68 | + |
| 69 | + UIView.animateWithDuration(0.4, animations: { |
| 70 | + self.pickerViewController.view.frame = self.offScreenFrame() |
| 71 | + }) { (complete: Bool) in |
| 72 | + self.pickerViewController.view.removeFromSuperview() |
| 73 | + self.presentingView = nil |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @objc private func uiKeyboardWillShow(notification: NSNotification) { |
| 78 | + |
| 79 | + } |
| 80 | + |
| 81 | + private func didTapCancelOnPickerViewController(pickerViewController: PickerViewController) { |
| 82 | + resign() |
| 83 | + } |
| 84 | + |
| 85 | + private func didTapDoneOnPickerViewController(pickerViewController: PickerViewController) { |
| 86 | + resign() |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + private static var manager = Manager() |
| 91 | + |
| 92 | + static func present(fromView: UIView, withTitle title: String?, configuration: UIPickerViewConfigurationBlock) { |
| 93 | + manager.present(fromView, withTitle: title, configuration: configuration) |
| 94 | + } |
| 95 | + |
| 96 | + static func present(fromView: UIView, withTitle title: String?, options: [String], selectedIndex: Int?, selectionHandler: UIPickerViewSelectionHandler) { |
| 97 | + |
| 98 | + } |
| 99 | + |
| 100 | + static func resign() { |
| 101 | + manager.resign() |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +public typealias UIPickerViewConfigurationBlock = (pickerView: UIPickerView) -> Void |
| 106 | +public typealias UIPickerViewSelectionHandler = (pickerView: UIPickerView, selectedItem: String, index: Int) -> Void |
| 107 | + |
| 108 | +internal protocol PickerViewControllerDelegate { |
| 109 | + func didTapCancelOnPickerViewController(pickerViewController: PickerViewController) |
| 110 | + func didTapDoneOnPickerViewController(pickerViewController: PickerViewController) |
| 111 | +} |
| 112 | + |
| 113 | +internal class PickerViewController: UIViewController { |
| 114 | + static let defaultToolbarHeight: CGFloat = 44.0 |
| 115 | + static let defaultPickerHeight: CGFloat = 216.0 |
| 116 | + static let defaultViewHeight: CGFloat = 260.0 |
| 117 | + static let defaultViewWidth: CGFloat = 320.0 |
| 118 | + |
| 119 | + private var flex = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: nil, action: nil) |
| 120 | + private var toolbarTitle = UIBarButtonItem(title: "", style: .Plain, target: nil, action: nil) |
| 121 | + private lazy var cancel: UIBarButtonItem = { |
| 122 | + [unowned self] in |
| 123 | + return UIBarButtonItem(barButtonSystemItem: .Cancel, target: self, action: #selector(PickerViewController.didTapDone(_:))) |
| 124 | + }() |
| 125 | + private lazy var done: UIBarButtonItem = { |
| 126 | + [unowned self] in |
| 127 | + return UIBarButtonItem(barButtonSystemItem: .Done, target: self, action: #selector(PickerViewController.didTapDone(_:))) |
| 128 | + }() |
| 129 | + private lazy var fixed: UIBarButtonItem = { |
| 130 | + let barButton = UIBarButtonItem(barButtonSystemItem: .FixedSpace, target: nil, action: nil) |
| 131 | + barButton.width = 8.0 |
| 132 | + return barButton |
| 133 | + }() |
| 134 | + |
| 135 | + lazy var picker: UIPickerView = { |
| 136 | + let view = UIPickerView(frame: CGRect(x: 0, y: PickerViewController.defaultToolbarHeight, width: PickerViewController.defaultViewWidth, height: PickerViewController.defaultPickerHeight)) |
| 137 | + view.translatesAutoresizingMaskIntoConstraints = false |
| 138 | + return view |
| 139 | + }() |
| 140 | + |
| 141 | + var delegate: PickerViewControllerDelegate? |
| 142 | + |
| 143 | + override func loadView() { |
| 144 | + self.view = UIView(frame: CGRect(x: 0, y: 0, width: PickerViewController.defaultViewWidth, height: PickerViewController.defaultViewHeight)) |
| 145 | + view.backgroundColor = UIColor.clearColor() |
| 146 | + |
| 147 | + let blurView = UIVisualEffectView(effect: UIBlurEffect(style: .ExtraLight)) |
| 148 | + blurView.translatesAutoresizingMaskIntoConstraints = false |
| 149 | + blurView.frame = view.bounds |
| 150 | + view.addSubview(blurView) |
| 151 | + |
| 152 | + view.addConstraint(NSLayoutConstraint(item: blurView, attribute: .Top, relatedBy: .Equal, toItem: view, attribute: .Top, multiplier: 1.0, constant: 0.0)) |
| 153 | + view.addConstraint(NSLayoutConstraint(item: blurView, attribute: .Bottom, relatedBy: .Equal, toItem: view, attribute: .Bottom, multiplier: 1.0, constant: 0.0)) |
| 154 | + view.addConstraint(NSLayoutConstraint(item: blurView, attribute: .Left, relatedBy: .Equal, toItem: view, attribute: .Left, multiplier: 1.0, constant: 0.0)) |
| 155 | + view.addConstraint(NSLayoutConstraint(item: blurView, attribute: .Right, relatedBy: .Equal, toItem: view, attribute: .Right, multiplier: 1.0, constant: 0.0)) |
| 156 | + |
| 157 | + let toolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: PickerViewController.defaultViewWidth, height: PickerViewController.defaultToolbarHeight)) |
| 158 | + toolbar.setItems([fixed, cancel, flex, toolbarTitle, flex, done, fixed], animated: false) |
| 159 | + toolbar.translatesAutoresizingMaskIntoConstraints = false |
| 160 | + view.addSubview(toolbar) |
| 161 | + |
| 162 | + toolbar.addConstraint(NSLayoutConstraint(item: toolbar, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .Height, multiplier: 1.0, constant: PickerViewController.defaultToolbarHeight)) |
| 163 | + view.addConstraint(NSLayoutConstraint(item: toolbar, attribute: .Top, relatedBy: .Equal, toItem: view, attribute: .Top, multiplier: 1.0, constant: 0.0)) |
| 164 | + view.addConstraint(NSLayoutConstraint(item: toolbar, attribute: .Left, relatedBy: .Equal, toItem: view, attribute: .Left, multiplier: 1.0, constant: 0.0)) |
| 165 | + view.addConstraint(NSLayoutConstraint(item: toolbar, attribute: .Right, relatedBy: .Equal, toItem: view, attribute: .Right, multiplier: 1.0, constant: 0.0)) |
| 166 | + |
| 167 | + view.addSubview(picker) |
| 168 | + picker.addConstraint(NSLayoutConstraint(item: picker, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .Height, multiplier: 1.0, constant: PickerViewController.defaultPickerHeight)) |
| 169 | + view.addConstraint(NSLayoutConstraint(item: picker, attribute: .Top, relatedBy: .Equal, toItem: toolbar, attribute: .Bottom, multiplier: 1.0, constant: 0.0)) |
| 170 | + view.addConstraint(NSLayoutConstraint(item: picker, attribute: .Left, relatedBy: .Equal, toItem: view, attribute: .Left, multiplier: 1.0, constant: 0.0)) |
| 171 | + view.addConstraint(NSLayoutConstraint(item: picker, attribute: .Right, relatedBy: .Equal, toItem: view, attribute: .Right, multiplier: 1.0, constant: 0.0)) |
| 172 | + } |
| 173 | + |
| 174 | + @objc func didTapCancel(sender: UIBarButtonItem) { |
| 175 | + if let delegate = self.delegate { |
| 176 | + delegate.didTapCancelOnPickerViewController(self) |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + @objc func didTapDone(sender: UIBarButtonItem) { |
| 181 | + if let delegate = self.delegate { |
| 182 | + delegate.didTapDoneOnPickerViewController(self) |
| 183 | + } |
| 184 | + } |
| 185 | +} |
0 commit comments