Skip to content

⏰ A customizable picker for selecting time values with hours, minutes, and/or seconds

License

Notifications You must be signed in to change notification settings

mac-gallagher/DurationPicker

Repository files navigation

DurationPicker

CI Documentation

DurationPicker is an iOS library that provides a customizable control for inputting time values ranging between 0 and 24 hours. It serves as a drop-in replacement of UIDatePicker with countDownTimer mode with additional functionality for time input.

A screenshot of a duration picker showing the selected value of 1 hour, 30 minutes, and 45 seconds.

Features

  • Styled to match UIDatePicker with countDownTimer mode
  • Multiple modes for selection of hours, minutes, and/or seconds
  • Option to specify intervals for hour, minute, and/or seconds
  • Support for minimum and maximum durations
  • Localization in 26+ languages
  • Built-in support for accessibility and VoiceOver

Usage

To use DurationPicker, simply create an instance of DurationPicker and add it to your view hierarchy. You can customize your picker using the following properties:

  • pickerMode: The mode of the picker, determines whether the duration picker allows selection of hours, minutes, and/or seconds
  • {hour|minute|second}Interval: The intervals at which the duration picker should display
  • {minimum|maximum}Duration: The minimum/maximum duration that the picker can show

The code below will produce the following duration picker.

A screenshot of a duration picker showing the selected value of 30 minutes and 30 seconds.

import DurationPicker

let picker = DurationPicker()
addSubview(picker)

picker.pickerMode = .minuteSecond
picker.minuteInterval = 5
picker.secondInterval = 30
picker.minimumDuration = (15 * 60) // 15 minutes
picker.maximumDuration = (45 * 60) + 30 // 45 minutes, 30 seconds

The selected duration can also be set programmatically through the duration property.

picker.duration = (30 * 60) + 30 // 30 minutes, 30 seconds

You can react to changes in the duration's picker value using UIActions.

let action = UIAction { [weak picker] _ in
    guard let picker else { return }
    print(picker.duration) // 1830
},
for: .primaryAction)
    
picker.addAction(action)

Demo

A screenshot of the demo project for DurationPicker.

To see DurationPicker in action, clone the repository and open the DurationPickerDemo project.

In addition to demonstrating the features of DurationPicker, the demo app serves as an example of how a DurationPicker can be used in a modern collection view.

Documentation

Full documentation available on Github Pages.

Installation

Swift Package Manager

DurationPicker is available through Swift PM. To install it, simply add the package as a dependency in Package.swift:

dependencies: [
  .package(url: "https://github.com/mac-gallagher/DurationPicker.git", from: "1.0.0"),
]

Manual

Download and drop the Sources directory into your project.

Requirements

  • iOS 15.0+
  • XCode 15.0+
  • Swift 5.9+

License

DurationPicker is available under the MIT license. See LICENSE for more information.

Contributing

Contributions are welcome! Fork the repo, make your changes, and submit a pull request.