InfinitePicker
is an customized infinite picker for iOS, it helps you to create a infinite picker using a customized cell.
InfinitePicker
also supports using with the RxSwift
(https://github.com/ReactiveX/RxSwift) extension.
To run the example project, clone the repo, and run pod install
from the Example directory first.
InfinitePicker is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'InfinitePicker'
The following code is a demo of a customized cell.
A customized cell class NumberPickerCell
is a subclass of the generic class InfinitePickerCell.
Int
is the model type of this customized cell.
The following property shoudl be overrided:
model
: Override the model property to set data for the cell.isSelected
: Override the isSelected property to set a selected effect.
class NumberPickerCell: InfinitePickerCell<Int> {
private lazy var numberLabel: UILabel = {
let label = UILabel()
label.textColor = .white
label.textAlignment = .center
label.font = UIFont.systemFont(ofSize: 23, weight: .bold)
return label
}()
override init(frame: CGRect) {
super.init(frame: frame)
addSubview(containerView)
createConstraints()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// Override the model property to set data for the cell.
override var model: Int? {
didSet {
guard let number = model else {
return
}
numberLabel.text = String(number)
}
}
// Override the isSelected property to set a selected effect.
override var isSelected: Bool {
didSet {
numberLabel.textColor = isSelected ? .yellow : . white
}
}
// ...
}
Create a picker in your view controller class. The generic type must be same as which in your customized class.
private lazy var numberPicker: RxInfinitePicker<Int> = {
let picker = RxInfinitePicker<Int>(
itemSize: CGSize(width: 50, height: 50),
scrollDirection: .vertical,
spacing: 10,
cellType: NumberPickerCell.self
)
picker.delegate = self
return picker
}()
Set your data to the picker.
override func viewDidLoad() {
super.viewDidLoad()
//...
numberPicker.items = Array(1 ... 9)
}
Get the selected item in the delegate InfinitePickerDelegate
.
extension CustomizedViewController: InfinitePickerDelegate {
func didSelectItem(at index: Int) {
numberLabel.text = "didSelectItem \(index)"
}
}
picker.rx.itemSelected.subscribe(onNext: { [unowned self] in
self.viewModel.pick(at: $0)
}).disposed(by: disposeBag)
viewModel.items.bind(to: picker.rx.items).disposed(by: disposeBag)
viewModel.selectedIndex.bind(to: picker.rx.selectedIndex).disposed(by: disposeBag)
lm2343635, lm2343635@126.com
InfinitePicker is available under the MIT license. See the LICENSE file for more info.