Latest 1.0
Homepage https://github.com/theodore-gallao/ItemPickerView
License MIT
Platforms ios 10.0
Authors

Swipe horizontally or tap to select through segmented items.

Demo
Applied Demo

ItemPickerView is a UIView subclass that functions as a picker for the items that it contains. Items may be selected by tapping or swiping (they may also be selected programatically), and the selected item will be positioned in the horizontal center of the view. Pass Item objects through the data source and handle events like selection through the delegate, much like UICollectionView or UITableView data sources and delegates. Since it is just a view, you can layout and constrain the ItemPickerView to any reasonable dimension, making it versatile and applicable to various scenarios.

Prerequisites

Swift 4.2+, iOS 10+, CocoaPods

Installation

Install with CocoaPods by adding the following to your Podfile:

pod 'ItemPickerView'

Usage

Import the framework

import ItemPickerView

Create an ItemPickerView object

var itemPickerView: ItemPickerView!

override func viewDidLoad() {
    super.viewDidLoad()

    self.itemPickerView = ItemPickerView()
}

Set ItemPickerView properties

override func viewDidLoad() {
    // ...

    self.itemPickerView.backgroundColor = UIColor.black
    self.itemPickerView.gradientColor = UIColor.black
    self.itemPickerView.gradientWidth = 75
    self.itemPickerView.translatesAutoresizingMaskIntoConstraints = false
}

Set delegate and data source. Be sure that they conform to the protocols ItemPickerViewDelegate and ItemPickerViewDataSource

override func viewDidLoad() {
    // ...

    self.itemPickerView.delegate = self
    self.itemPickerView.dataSource = self
} 

Layout the ItemPickerView

override func viewDidLoad() {
    // ...

    self.view.addSubview(self.itemPickerView)
    self.itemPickerView.heightAnchor.constraint(equalToConstant: 44).isActive = true
    self.itemPickerView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor, constant: 0).isActive = true
    self.itemPickerView.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: 0).isActive = true
    self.itemPickerView.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: 0).isActive = true
} 

Set the items

var items: [Item]!

override func viewDidLoad() {
    // ...

    let item = Item(text: "Item", selectedColor: UIColor.yellow)
    let anotherItem = Item(text: "Another Item", font: UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.bold), color: UIColor.cyan.withAlphaComponent(0.7), selectedColor: UIColor.cyan)
    let lastItem = Item(text: "Last Item", color: UIColor.orange.withAlphaComponent(0.7), selectedColor: UIColor.orange)
    self.items = [item, anotherItem, lastItem]
} 

Implement the data source functions, and handle the delegate functions(optional). Be sure that they conform to the protocols ItemPickerViewDelegate and ItemPickerViewDataSource

// Return number of items
func itemPickerViewNumberOfItems(_ itemPickerView: ItemPickerView) -> Int {
    return self.items.count
}

// Return Item at index
func itemPickerView(_ itemPickerView: ItemPickerView, itemAtIndex index: Int) -> Item {
    return self.items[index]
}

// Called when item is selected
func itemPickerView(_ itemPickerView: ItemPickerView, didSelectItemAtIndex index: Int) {
    // Handle item selection at given index.
}

Appearance

Change the appearance properties of ItemPickerView and Item

ItemPickerView

Appearance properties of ItemPickerView.

Gradient Width – The width of the left and right gradients, default 100, set to 0 to remove gradients

self.itemPickerView.gradientWidth = 100

Gradient Color – The color of the left and right gradients, default UIColor.black. Ideally, set this to the same value as self.itemPickerView.backgroundColor

self.itemPickerView.gradientColor = UIColor.black

Item

Appearance properies of Item, default ""

Text – The text of this item

let item = Item()
item.text = ""

Font – The font of this item’s text, default UIFont.systemFont(ofSize: 15, weight: UIFont.Weight.semibold)

let item = Item()
item.font = UIFont.systemFont(ofSize: 15, weight: UIFont.Weight.semibold)

Color – The text color of this item, default UIColor.gray

let item = Item()
item.color = UIColor.gray

Selected Color – The selected color of this item, default Default UIColor.black

let item = Item()
item.selectedColor = Default UIColor.black

Authors

Theodore GallaoGitHub

License

This project is under the MIT license. See LICENSE.md.

Latest podspec

{
    "name": "ItemPickerView",
    "version": "1.0",
    "summary": "Swipe horizontally to select through segmented items.",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "homepage": "https://github.com/theodore-gallao/ItemPickerView",
    "authors": {
        "Theodore Gallao": "[email protected]"
    },
    "source": {
        "git": "https://github.com/theodore-gallao/ItemPickerView.git",
        "tag": "1.0"
    },
    "source_files": "ItemPickerView/*.swift",
    "platforms": {
        "ios": "10.0"
    },
    "swift_version": "4.2",
    "ios": {
        "frameworks": "UIKit"
    }
}

Pin It on Pinterest

Share This