RxInfinitePicker 0.1

RxInfinitePicker 0.1

Maintained by [Meng Li].



  • By
  • lm2343635

RxInfinitePicker

CI Status Version License Platform

RxInfinitePicker is an iOS infinite picker based on RxSwift, it helps you to create a infinite picker using a customized cell. Using RxInfinitePicker requires the base RxSwift (https://github.com/ReactiveX/RxSwift) knowledge.

Example

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

Documentation

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

pod 'RxInfinitePicker'

Customized cell

The following code is a demo of a customized cell. A customized cell class NumberPickerCell is a subclass of the generic class RxInfinitePickerCell. Int is the model type of this customized cell.

class NumberPickerCell: RxInfinitePickerCell<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 var model: Int? {
        didSet {
            guard let number = model else {
                return
            }
            numberLabel.text = String(number)
        }
    }

    // ...
    
}

Using RxInfinitePicker

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,
        cellType: NumberPickerCell.self
    )
    picker.itemSelected.subscribe(onNext: { [unowned self] in
        print("itemSelected \($0)")
    }).disposed(by: disposeBag)
    return picker
}()

Bind your data to the picker.

override func viewDidLoad() {
    super.viewDidLoad()
    //...

    viewModel.items.bind(to: numberPicker.items).disposed(by: disposeBag)
}

After scrolling or selecting a new item, the itemSelected signal that contains the selected model will be updated.

Author

lm2343635, [email protected]

License

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