Write a quick pop-up multiple roller selector (PickerView) gadget

Use this small framework to quickly complete the registration page or personal information page selection

Example effect

TextField use example

Write a quick pop-up multiple roller selector (PickerView) gadget
TextField using the example.Gif

Button use example (GIF effect is not satisfied with < ####>)

Write a quick pop-up multiple roller selector (PickerView) gadget
button using sample.Gif

Can be simple and fast to achieve the effect of the above

Usage method

Write a quick pop-up multiple roller selector (PickerView) gadget
set data.Png

1 TextField support Xib code generation and use, simply call a method, set the selected data, and the default selected item (optional setting), you can set whether to automatically fill the selected value in the rolling time, and then click Finish response in closure

Write a quick pop-up multiple roller selector (PickerView) gadget
TextField using.Png

The 2 button (click event) in use, only need to use UsefulPickerView class method in the corresponding click events, parameters and the use of these methods and TextField have the same effect, one is to click the
button to remove background selector Write a quick pop-up multiple roller selector (PickerView) gadget using.Png

Implementation analysis

The realization of TextField is more convenient, because the system default is when a pop-up keyboard click, and allow us to modify his inputView, so only need to set inputView to TextField we want to pop up pickerView. TextField was also set to not respond to input and display the cursor, you only need to override the effect. A method of adding method. Build the PickerView for TextField, the first is when his initialization is initialized and set up his inputView as required by PickerView, but after taking into account, only when the user clicks on the input box, began to edit the settings on the proxy method, then in order to avoid too much in the end the consumption, editing method, the destruction of the inputView. was originally the proxy method in the realization of TextField pickerView, But later, when considering the use of buttons also need to implement these agent method, so it will be PickerView and ToolBar separately, easy to reuse and code separation. Because TextField and pickerView proxy method to separate, to achieve the same step set selected when the user rolls the data to the TextField will need to use the agent (Closure) pickerView will pass the selected data to the TextField, so a PickerViewDelegate ToolBar protocol this part is relatively simple, only need a cancel button and display the title and determine the Label can, also need to provide external response method click event (proxy or Closure) implementation of PickerView is a little bit more complicated, because to separate data processing, multiple columns are not linked data, multi linked data, city selection, date selection. Need to implement the Pick The erView related method to set up data, and display data in response to the selected data in these methods, which the author uses switch to different display modes (single data, multi column irrelevant data…) conducted a similar treatment. The use of questions about the parameters of the array, the first is to use NSArray to achieve because the data is stored in the row, String, data stored in an array of columns, columns of linked data is saved in the array and the dictionary, so NSA still has to achieve more convenient, but still want to use swift Array, so there is the use of [Any], the array of ideas, but it can’t come detection of the input data format is correct by IDE, so we separate each are used in an array to realize the convenience and safety for comparison. The use of
10 button Popup When the selector, because unlike TextField itself can be out of view, so it chose to use the current Window to pop up a View to display pickerView, also need to remove this pop-up View in the selection is completed or click on the background, need to use a little animation effect to the transition.

Code implementation part

Code more, see the specific source address

Part TextField

Private func commonInit (notice / monitor) {NSNotificationCenter.defaultCenter (.AddObserver (self), selector: #selector (self.didBeginEdit), name: UITextFieldTextDidBeginEditingNotification, object: self (NSNotificationCenter.defaultCenter).AddObserver (self), selector: #selector (self.didEndEdit), name: UITextFieldTextDidEndEditingNotification, object: self)} / / to add pickerView func didBeginEdit (Editor) {let pickerView = setUpPickerClosure (?.delegate = self pickerView)? InputView = pickerView pickerView func} / / editor to complete the destruction of didEndEdit (inputView = Nil) {} / / override public func do not display the input cursor caretRectForPosition (position: UITextPosition -> CGRect) {return CGRectZero} / / save in this closure, the editor of the times began in the implementation, to avoid direct initialization before here in pickerView, each SelectionTextField when calling this method to initialize pickerView, when there is more than one pickerView is setUpPickerClosure = {memory consumption () -> PickerView in return PickerView.singleColPicker (toolBarTitle, singleColData: data, defaultIndex: defaultSelectedIndex, cancelAction: {[unowned self] in self.endEditing (true)}, doneAction: {[unowned self] (selectedIndex: Int, selectedValue: String -> Void in); doneAction (textField:self, selectedIndex: selectedIndex?, SelectedValue: selectedValue) self.endEditing (true)})

UsefulPickerView. handles eject and remove view

Private func (showPicker) by window view let {/ / window = UIApplication.sharedApplication (POP).KeyWindow guard let currentWindow window else = {return} currentWindow.addSubview (self) UIView.animateWithDuration (0.25, animations: unowned self] in {[self.backgroundColor = UIColor (red: 0, green: 0, blue: 0, alpha: 0.1) self.pickerView.frame = self.showFrame}, completion: nil) func (hidePicker) {} / / remove self from window in UIView.animateWithDuration (0.25, animations: self] in self.backgroundColor {[unowned = UIColor.clearColor (self.pickerView.frame = self.hideFrame)}) {[unowned self] (in _) Self.removeFromSuperview ()}}

PickerView code on a lot, and here do not post it

Details and Demo, please see the source code address, if you feel helpful, might as well give a star to encourage it, welcome attention, welcome to exchange