|
| 1 | +# WatchKitTimePicker |
| 2 | + |
| 3 | +**WatchKitTimePicker** is a time picker data source for WatchKit that... |
| 4 | + - Mirrors the behavior of UIKit's `UIDatePicker` |
| 5 | + - Automatically uses either 12-hour or 24-hour time, depending on the user's current Locale. |
| 6 | + - Supports watchOS 2.0+ |
| 7 | + |
| 8 | +## Demo |
| 9 | + |
| 10 | +<p align="center"> |
| 11 | + <img src="images/watchkit time picker 12hr.gif" width="300px"> <img src="images/watchkit time picker 24hr.gif" width="300px"> |
| 12 | +</p> |
| 13 | + |
| 14 | +<br> |
| 15 | + |
| 16 | + |
| 17 | +## Installation |
| 18 | + |
| 19 | +#### Manual Installation: |
| 20 | + |
| 21 | +**WatchKitTimePicker** is just one individual .swift file: [`TimePickerDataSource.swift`](https://github.com/calda/WatchKitTimePicker/blob/master/WatchKitTimePicker/TimePickerDataSource.swift). You could install it quickly by downloading that file and dragging it in to your Watch App Extension target. |
| 22 | + |
| 23 | +#### [Carthage](https://github.com/Carthage/Carthage): |
| 24 | + |
| 25 | +Add `github "calda/WatchKitTimePicker"` to your Cartfile. |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +Unlike with iOS view-layer libraries, you can't just distribute and use a `UIView` or `WKInterfaceObject` subclass. Interface elements have to be set up using Interface Builder. |
| 30 | + |
| 31 | +**WatchKitTimePicker** is a data source that controls and manages a group of `WKInterfacePicker` objects. |
| 32 | + |
| 33 | +### Interface Builder: |
| 34 | + |
| 35 | +- Create a horizontal `WKInterfaceGroup`. |
| 36 | +- Add three `WKInterfacePicker` objects to the group. |
| 37 | +- Connect and `@IBOutlet` and an `@IBAction` for each of the pickers. |
| 38 | + |
| 39 | +### Your `WKInterfaceController` subclass: |
| 40 | + |
| 41 | +```Swift |
| 42 | +import WatchKit |
| 43 | +import Foundation |
| 44 | +import WatchKitTimePicker |
| 45 | + |
| 46 | +class InterfaceController: WKInterfaceController { |
| 47 | + |
| 48 | + var timePickerDataSource: TimePickerDataSource! |
| 49 | + @IBOutlet weak var hourTimePicker: WKInterfacePicker! |
| 50 | + @IBOutlet weak var minuteTimePicker: WKInterfacePicker! |
| 51 | + @IBOutlet weak var amPmTimePicker: WKInterfacePicker! |
| 52 | + |
| 53 | + override func awake(withContext context: Any?) { |
| 54 | + timePickerDataSource = TimePickerDataSource( |
| 55 | + hoursPicker: hourTimePicker, |
| 56 | + minutesPicker: minuteTimePicker, |
| 57 | + amPmPicker: amPmTimePicker) |
| 58 | + |
| 59 | + timePickerDataSource.selectedTimeDidUpdate = { selectedTime in |
| 60 | + // ... |
| 61 | + } |
| 62 | + |
| 63 | + timePickerDataSource.setup(withInitiallySelectedDate: Date()) |
| 64 | + } |
| 65 | + |
| 66 | + @IBAction func hourPickerDidUpdate(_ index: Int) { |
| 67 | + timePickerDataSource.hourPickerUpdated(to: index) |
| 68 | + } |
| 69 | + |
| 70 | + @IBAction func minutePickerDidUpdate(_ index: Int) { |
| 71 | + timePickerDataSource.minutePickerUpdated(to: index) |
| 72 | + } |
| 73 | + |
| 74 | + @IBAction func amPmPickerDidUpdate(_ index: Int) { |
| 75 | + timePickerDataSource.amPmPickerUpdated(to: index) |
| 76 | + } |
| 77 | + |
| 78 | +} |
| 79 | + |
| 80 | +``` |
0 commit comments