InfinitePicker 0.2.5

InfinitePicker 0.2.5

Maintained by [Meng Li].



  • By
  • lm2343635

RxInfinitePicker

CI Status Version License Platform

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.

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Documentation

InfinitePicker is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'InfinitePicker'

Customized cell

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
        }
    }
    
    // ...
    
}

Using InfinitePicker

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)"
    }
}

Using with the RxSwift extension.

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)

Author

lm2343635, [email protected]

License

InfinitePicker is available under the MIT license. See the LICENSE file for more info.