Latest 0.2.5
Homepage https://github.com/lm2343635/InfinitePicker
License MIT
Platforms ios 9.0
Dependencies InfiniteLayout
Authors

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.

InfinitePickerInfinitePicker

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]126.com

License

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

Latest podspec

{
    "name": "InfinitePicker",
    "version": "0.2.5",
    "summary": "An iOS customized infinite picker.",
    "description": "InfinitePicker is an customized infinite picker for iOS, it helps you to create a infinite picker using a customized cell.",
    "homepage": "https://github.com/lm2343635/InfinitePicker",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "lm2343635": "[email protected]"
    },
    "source": {
        "git": "https://github.com/lm2343635/InfinitePicker.git",
        "tag": "0.2.5"
    },
    "platforms": {
        "ios": "9.0"
    },
    "dependencies": {
        "InfiniteLayout": [
            "~> 0.2"
        ]
    },
    "default_subspecs": "Core",
    "subspecs": [
        {
            "name": "Core",
            "source_files": "InfinitePicker/Classes/Core/**/*"
        },
        {
            "name": "Rx",
            "dependencies": {
                "InfinitePicker/Core": [
                    "~> 0"
                ],
                "RxCocoa": [
                    "~> 4.4"
                ]
            },
            "source_files": "InfinitePicker/Classes/Rx/**/*"
        }
    ]
}

Pin It on Pinterest

Share This