今からお前んちこいよ

多摩川沿いにて細々とお勉強。

swiftで便利なModalPickerライブラリ [PKPicker]

概要

ちょっとしたリスト選択や日付選択をやるのが意外と面倒で、
いい感じにモーダルで出してくれるライブラリが欲しかったので作ってみた。
 
list: リスト表示形式、ラジオボックスで一つ、チェックボックスで複数選択できる
drum: 縦にくるくるするやつ。何項目でもおk
date:普通にUIDatePickerをモーダルに貼ったやつ
clock: Androidのgoogleアプリに見られる時計型の時刻選択UI

github.com

PKPicker

List

Single

f:id:hakopako03:20161020221056p:plain

PKPicker.list(title: "Interest",
              items: ["Art", "Music", "Sports", "Travel"],
              defaultSelected: [1],
              allowMulti: false,
              action: {(m, obj) -> Bool in
                return true
            }
)
Multi

f:id:hakopako03:20161020221106p:plain

PKPicker.list(title: "Interest",
              items: ["Art", "Music", "Sports", "Travel"],
              defaultSelected: [1],
              allowMulti: true,
              action: {(m, obj) -> Bool in
                if obj.count == 0 {
                    m?.text = "Select at least one."
                    m?.textColor = UIColor.red
                    return false
                }
                return true
            }
)

Drum

f:id:hakopako03:20161020221203p:plain

let arr1 = ["T-shirts", "Parker", "Shoes", "Hat", "Bag"]
let arr2 = ["0", "1", "2", "3"]
let itemsArr:[[String]] = [arr1, arr2]
PKPicker.drum(title: "Item",
                items: itemsArr,
                defaultSelected: [0, 0],
                action: {(m, obj) -> Bool in
                    m?.text = "Sorry this item is sold out."
                    m?.textColor = UIColor.red
                    return false

                }
)

Date

f:id:hakopako03:20161020221212p:plain

PKPicker.date(title: "Date",
              format: UIDatePickerMode.dateAndTime,
              defaultSelected: Date(),
              action: { (m, d) -> Bool in
                if NSDate().compare(d) == ComparisonResult.orderedAscending{
                    m?.text = "Pick past date."
                    m?.textColor = UIColor.red
                    return false
                }
                return true
              }
)

clock

12時間表記

f:id:hakopako03:20161020221221p:plain

PKPicker.clock(title: "Start time",
               twentyFourHour: false,
               defaultSelected: (8, 12, "AM"),
               action: { (m, d) -> Bool in
                if d.0 < 7 && d.2 == "AM" {
                    m?.text = "Too early to start."
                    m?.textColor = UIColor.red
                    return false
                }
                return true
    }
)
24時間表記

f:id:hakopako03:20161115112231p:plain

PKPicker.clock(title: "Start time",
               twentyFourHour: true,
               defaultSelected: (8, 12, ""),
               action: { (m, d) -> Bool in
                if d.0 < 7 {
                    m?.text = "Too early to start."
                    m?.textColor = UIColor.red
                    return false
                }
                return true
    }
)

共通設定

PKPicker.tintColor:UIColor        // default: UIColor(colorLiteralRed: 0, green: 122/255, blue: 1, alpha: 1)
PKPicker.titleFontColor:UIColor   // default: UIColor.darkGray
PKPicker.titleFontStyle:UIFont    // default: UIFont.boldSystemFont(ofSize: 17)
PKPicker.messageFontColor:UIColor // default: UIColor.gray
PKPicker.messageFontStyle:UIFont  // default: UIFont.systemFont(ofSize: 13)
PKPicker.buttonFontColor:UIColor  // default: UIColor.gray
PKPicker.backgroundColor:UIColor  // default: UIColor.white
PKPicker.doneButtonText:String    // default: "Done"
PKPicker.cancelButtonText:String  // default: "Cancel"
PKPicker.width:CGFloat            // default: 300

 

\時計型使っていってほしいー/