Skip to content

Commit f7a929e

Browse files
committed
Add support for callbacks
1 parent 1c81dab commit f7a929e

File tree

4 files changed

+121
-39
lines changed

4 files changed

+121
-39
lines changed

Demo/KWStepperDemo/Controllers/ViewController.swift

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ class ViewController: UIViewController, KWStepperDelegate {
3131
decrementButton: decrementButton,
3232
incrementButton: incrementButton)
3333

34-
stepper.addTarget(self,
35-
action: "stepperDidChange",
36-
forControlEvents: .ValueChanged)
37-
3834
stepper.autoRepeat = true
3935
stepper.autoRepeatInterval = 0.10
4036
stepper.wraps = true
@@ -45,8 +41,20 @@ class ViewController: UIViewController, KWStepperDelegate {
4541
stepper.decrementStepValue = 1
4642

4743
stepper.delegate = self
44+
45+
stepper.valueChangedCallback = {
46+
self.countLabel.text = NSString(format: "%.f", self.stepper.value)
47+
}
48+
49+
stepper.decrementCallback = {
50+
println("decrementCallback: The stepper did decrement")
51+
}
52+
53+
stepper.incrementCallback = {
54+
println("incrementCallback: The stepper did increment")
55+
}
4856
}
49-
57+
5058
func configureSwitches() {
5159
wrapsSwitch.on = stepper.wraps
5260
autoRepeatSwitch.on = stepper.autoRepeat
@@ -60,16 +68,28 @@ class ViewController: UIViewController, KWStepperDelegate {
6068
forControlEvents: .ValueChanged)
6169
}
6270

63-
// MARK: KWStepper Events
64-
65-
func stepperDidChange() {
66-
countLabel.text = NSString(format: "%.f", stepper.value)
71+
// MARK: KWStepperDelegate
72+
73+
func KWStepperDidDecrement() {
74+
}
75+
76+
func KWStepperDidIncrement() {
77+
}
78+
79+
func KWStepperMaxValueClamped() {
80+
println("KWStepperDelegate: Max value clamped")
81+
stepperDidClampValue()
82+
}
83+
84+
func KWStepperMinValueClamped() {
85+
println("KWStepperDelegate: Min value clamped")
86+
stepperDidClampValue()
6787
}
6888

6989
func stepperDidClampValue() {
7090
let minValue = NSString(format: "%.f", stepper.minimumValue)
7191
let maxValue = NSString(format: "%.f", stepper.maximumValue)
72-
92+
7393
UIAlertView(
7494
title: "Stepper Limit Reached",
7595
message: "The step value was clamped, as it must be between \(minValue) and \(maxValue).",
@@ -78,26 +98,6 @@ class ViewController: UIViewController, KWStepperDelegate {
7898
).show()
7999
}
80100

81-
// MARK: KWStepperDelegate Methods
82-
83-
func KWStepperDidDecrement() {
84-
println("The stepper did decrement")
85-
}
86-
87-
func KWStepperDidIncrement() {
88-
println("The stepper did increment")
89-
}
90-
91-
func KWStepperMaxValueClamped() {
92-
println("Max value clamped")
93-
stepperDidClampValue()
94-
}
95-
96-
func KWStepperMinValueClamped() {
97-
println("Min value clamped")
98-
stepperDidClampValue()
99-
}
100-
101101
// MARK: UISwitch Events
102102

103103
func switchDidChange(sender: UISwitch) {

KWStepper.podspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = "KWStepper"
3-
s.version = "1.0.1"
3+
s.version = "1.2.0"
44
s.summary = "A stepper control with flexible UI and tailored UX."
55
s.homepage = "https://github.com/kyleweiner/KWStepper"
66
s.license = { :type => "MIT", :file => "LICENSE" }
@@ -11,5 +11,5 @@ Pod::Spec.new do |s|
1111
s.platform = :ios
1212
s.ios.deployment_target = "8.0"
1313
s.requires_arc = true
14-
s.description = "KWStepper is a stepper control written in Swift. Unlike UIStepper, KWStepper allows for a fully customized UI and provides optional delegate methods for tailoring the UX."
14+
s.description = "KWStepper is a stepper control written in Swift. Unlike UIStepper, KWStepper allows for a fully customized UI and provides callbacks for tailoring the UX."
1515
end

KWStepper/KWStepper.swift

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,18 @@ class KWStepper: UIControl {
6666
if let delegate = delegate {
6767
delegate.KWStepperDidIncrement?()
6868
}
69+
70+
if let callback = incrementCallback {
71+
callback()
72+
}
6973
} else {
7074
if let delegate = delegate {
7175
delegate.KWStepperDidDecrement?()
7276
}
77+
78+
if let callback = decrementCallback {
79+
callback()
80+
}
7381
}
7482

7583
if value < minimumValue {
@@ -79,6 +87,10 @@ class KWStepper: UIControl {
7987
}
8088

8189
sendActionsForControlEvents(.ValueChanged)
90+
91+
if let callback = valueChangedCallback {
92+
callback()
93+
}
8294
}
8395
}
8496

@@ -130,6 +142,31 @@ class KWStepper: UIControl {
130142
}
131143
}
132144

145+
/**
146+
Executed when the value is changed.
147+
*/
148+
var valueChangedCallback: (() -> ())?
149+
150+
/**
151+
Executed when the value is decremented.
152+
*/
153+
var decrementCallback: (() -> ())?
154+
155+
/**
156+
Executed when the value is incremented.
157+
*/
158+
var incrementCallback: (() -> ())?
159+
160+
/**
161+
Executed when the max value is clamped.
162+
*/
163+
var maxValueClampedCallback: (() -> ())?
164+
165+
/**
166+
Executed when the min value is clamped.
167+
*/
168+
var minValueClampedCallback: (() -> ())?
169+
133170
var delegate:KWStepperDelegate? = nil
134171

135172
// MARK: Private Variables
@@ -171,6 +208,10 @@ class KWStepper: UIControl {
171208
if let delegate = delegate {
172209
delegate.KWStepperMinValueClamped?()
173210
}
211+
212+
if let callack = minValueClampedCallback {
213+
callack()
214+
}
174215
}
175216
}
176217

@@ -189,6 +230,10 @@ class KWStepper: UIControl {
189230
if let delegate = delegate {
190231
delegate.KWStepperMaxValueClamped?()
191232
}
233+
234+
if let callback = maxValueClampedCallback {
235+
callback()
236+
}
192237
}
193238
}
194239

README.md

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,21 @@
44
[![License](https://img.shields.io/cocoapods/l/KWStepper.svg?style=flat)](https://raw.githubusercontent.com/kyleweiner/KWStepper/master/LICENSE)
55
[![Platform](https://img.shields.io/cocoapods/p/KWStepper.svg?style=flat)](http://cocoapods.org/?q=kwstepper)
66

7-
KWStepper is a stepper control written in Swift. Unlike [UIStepper](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIStepper_Class/index.html), KWStepper allows for a fully customized UI and provides optional delegate methods for tailoring the UX.
7+
KWStepper is a stepper control written in Swift. Unlike [UIStepper](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIStepper_Class/index.html), KWStepper allows for a fully customized UI and provides callbacks for tailoring the UX.
88

99
![KWStepper Screenshot](screenshots.png)
1010

1111
KWStepper was initially created in Objective-C for [Addo Label's](http://addolabel.com/) [Counters•](https://itunes.apple.com/app/id722416562?mt=8) and is now available in Swift for you to enjoy :)
1212

13+
## Features
14+
15+
* Allows for a fully customized UI.
16+
* Provides properties for setting different decrement and increment steps.
17+
* Offers optional callbacks for responding to control events and tailoring the UX.
18+
1319
## Installation
1420

15-
KWStepper is available on [CocoaPods](http://cocoapods.org), but it doesn't yet work per [this issue](https://github.com/CocoaPods/CocoaPods/issues/2226). For now, simply copy the files in the `KWStepper` directory into your project.
21+
Simply copy the files in the `KWStepper` directory into your project.
1622

1723
## Usage
1824

@@ -30,7 +36,19 @@ var stepper: KWStepper!
3036
stepper = KWStepper(
3137
decrementButton: decrementButton,
3238
incrementButton: incrementButton)
39+
```
40+
41+
Respond to control events using the `valueChangedCallback` property.
42+
43+
```swift
44+
stepper.valueChangedCallback = {
45+
self.countLabel.text = NSString(format: "%.f", self.stepper.value)
46+
}
47+
```
48+
49+
Or, use the target-action pattern.
3350

51+
```swift
3452
stepper.addTarget(self,
3553
action: "stepperDidChange",
3654
forControlEvents: .ValueChanged)
@@ -42,13 +60,20 @@ func stepperDidChange() {
4260
}
4361
```
4462

45-
### Properties
63+
### Configuring KWStepper
4664

47-
The properties of KWStepper are identical to [UIStepper](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIStepper_Class/index.html) with the following exceptions:
65+
With the exception of the `continuous` property, KWStepper offers everything provided by [UIStepper](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIStepper_Class/index.html) and more.
4866

49-
* Instead of a single `stepValue` property, KWStepper provides `decrementStepValue` and `incrementStepValue` properties for setting different decrement and increment steps.
50-
* There is a `delegate` property that should be set when adhering to the [`KWStepperDelegate`](#kwstepperdelegate) protocol.
51-
* During user interaction, change events are sent immediately when the value changes–there is no `continuous` property.
67+
```swift
68+
stepper.autoRepeat = true
69+
stepper.autoRepeatInterval = 0.10
70+
stepper.wraps = true
71+
stepper.minimumValue = 0
72+
stepper.maximumValue = 8
73+
stepper.value = 0
74+
stepper.incrementStepValue = 1
75+
stepper.decrementStepValue = 1
76+
```
5277

5378
### KWStepperDelegate
5479

@@ -63,6 +88,18 @@ In the demo, `KWStepperMaxValueClamped()` and `KWStepperMinValueClamped()` are u
6388

6489
In [Counters•](https://itunes.apple.com/app/id722416562?mt=8), `KWStepperDidDecrement()` and `KWStepperDidIncrement()` are used to play different sounds when decrementing and incrementing the steppers.
6590

91+
### Callbacks
92+
93+
As an alternative to the `KWStepperDelegate` protocol, KWStepper provides the following callbacks:
94+
95+
* `valueChangedCallback`
96+
* `decrementCallback`
97+
* `incrementCallback`
98+
* `maxValueClampedCallback`
99+
* `minValueClampedCallback`
100+
101+
102+
66103
## Author
67104

68105
KWStepper was written by Kyle Weiner.

0 commit comments

Comments
 (0)