KWStepper

2.5.3

A stepper control with flexible UI and tailored UX.
kyleweiner/KWStepper

What's New

Version 2.5.3

2019-11-04T06:03:24Z

Adds support for Swift Package Manager (#18).

KWStepper

Version Carthage Documentation Platform License

KWStepper is a stepper control written in Swift. Unlike UIStepper, KWStepper allows for a fully customized UI and provides callbacks for tailoring the UX.

KWStepper Screenshot

KWStepper was initially created in Objective-C for Addo Label's Counters• and is now available in Swift for you to enjoy 😁

Features

  • Allows for a fully customized UI.
  • Provides properties for setting different decrement and increment steps.
  • Offers optional callbacks for responding to control events and tailoring the UX.
  • Supports method chaining for easier configuration and event handling.

Installation

CocoaPods

KWStepper is available through CocoaPods. To install it, simply add the following lines to your Podfile:

use_frameworks!
pod 'KWStepper'

Carthage

To integrate KWStepper using Carthage, add the following line to you Cartfile:

github "kyleweiner/KWStepper"

Swift Package Manager

To add KWStepper to your Xcode project via Swift Package Manager, select File → Swift Packages → Add Package Dependency… and enter the URL for this this repository.

Manually

If you prefer not to use a dependency manager, simply copy the source files into your project.

Usage

Try the example project!

var stepper: KWStepper!

@IBOutlet weak var countLabel: UILabel!
@IBOutlet weak var decrementButton: UIButton!
@IBOutlet weak var incrementButton: UIButton!
stepper = KWStepper(decrementButton: decrementButton, incrementButton: incrementButton)

Respond to control events using the valueChangedCallback property.

stepper.valueChangedCallback = { stepper in
    self.countLabel.text = String(stepper.value)
}

Or, use the target-action pattern.

stepper.addTarget(self, action: #selector(stepperDidChange), for: .valueChanged)

Configuring KWStepper

With the exception of the continuous property, KWStepper offers everything provided by UIStepper and more.

stepper.autoRepeat = true
stepper.autoRepeatInterval = 0.10
stepper.wraps = true
stepper.minimumValue = 0
stepper.maximumValue = 8
stepper.value = 0
stepper.incrementStepValue = 1
stepper.decrementStepValue = 1

Method chaining is also supported:

stepper
    .wraps(true)
    .maximumValue(10)
    .stepValue(2)
    .valueChanged { stepper in
        // ...
    }

If necessary, the rounding behavior for incrementing and decrementing may be modified via roundingBehavior.

KWStepperDelegate

Adopting KWStepperDelegate provides the following optional delegate methods for tailoring the UX.

  • optional func KWStepperDidDecrement()
  • optional func KWStepperDidIncrement()
  • optional func KWStepperMaxValueClamped()
  • optional func KWStepperMinValueClamped()
  • optional func KWStepperDidEndLongPress()

Callbacks

KWStepper provides the following callbacks:

  • valueChangedCallback
  • decrementCallback
  • incrementCallback
  • maxValueClampedCallback
  • minValueClampedCallback
  • longPressEndedCallback

Method chaining is supported for callbacks too:

stepper
    .valueChanged { _ in }
    .didDecrement { _ in }
    .didIncrement { _ in }
    .maxValueClamped { _ in }
    .minValueClamped { _ in }
    .longPressEnded { _ in }

// `maxValueClampedCallback` and `minValueClampedCallback` may be set simultaneously.
stepper.valueClamped { stepper in
    // ...
}

In the example project, valueChanged(_:) is used to update the count label text when the stepper value changes. valueClamped(:_) is used to present a UIAlertController when a limit is reached and the wraps property is set to false.

Author

KWStepper was written by Kyle Weiner and contributors.

License

KWStepper is available under the MIT license. See the LICENSE file for details.

Description

  • Swift Tools 5.0.0
View More Packages from this Author

Dependencies

  • None
Last updated: Sun Aug 27 2023 07:26:55 GMT-0900 (Hawaii-Aleutian Daylight Time)