Support this project via Gratipay
A simple vertical stepper control for iOS that is similar in function to the UIStepper control. Personally, I think it was short-sighted of Apple to only release a horizontal stepper control, so this was created to remedy that.
Note: RPVerticalStepper is a subclass of UIControl so it inherits all characteristics of a typical control object.
- Uses floats so your output can be either a float value or you can change the precision (via NSString's
stringWithFormatmethod) to show it as whole number. - Set minimum & maximum values for the stepper. When the stepper reaches either of these limits, it automatically disables the button and prevents the user from surpassing the limit.
- Turn on auto-repeat to keep incrementing/decrementing the value for as long as the user's pressing on the button.
- Change the auto-repeat interval so that the stepper will change faster or slower.
- You can add the control to a NIB/Storyboard or you can add it to the subview programmatically.
value(CGFloat, defaults to 1) : The current value of the stepper controlminimumValue(CGFloat, defaults to 1) : The minimum value the stepper value can be before the decrement button is disabledmaximumValue(CGFloat, defaults to 99) : The maximum value the stepper value can be before the increment button is disabledstepValue(CGFloat, defaults to 1) : The amount that thevalueis changed by when the increment or decrement buttons are pressedautoRepeat(BOOL, defaults toYES) : Sets whether the stepper should keep changing the value for as long as the user's finger is pressed down on the controlautoRepeatInterval(CGFloat, defaults to 0.5 seconds) : Sets the interval at which auto-repeat should change the stepper value
- Use the control in the standard way and hook up an
IBActionto theValue Changedevent in Interface Builder - Or have your view controller conform to the
RPVerticalStepperDelegateprotocol and then use thestepperValueDidChange:method to know when the stepper value has changed - Both of the above methods are shown in the example project
The code base comes with an example project:
- Example: A simple example showing how to use use the stepper control in two different ways
And here is a basic example assuming you've added the control to a NIB or Storyboard file:
In the .h header file:
#import <UIKit/UIKit.h> #import "RPVerticalStepper.h" @interface ViewController : UIViewController @property (nonatomic, weak) IBOutlet UILabel *stepperLabel; @property (nonatomic, weak) IBOutlet RPVerticalStepper *stepper; @end
In the .m implementation file:
#import "ViewController.h" @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Set some different defaults self.stepper.value = 1.0f; self.stepper.minimumValue = -100.0f; self.stepper.maximumValue = 100.0f; self.stepper.stepValue = 5.0f; self.stepper.autoRepeatInterval = 0.1f; } // This is called from the control event hooked up to the control in the Storyboard - (IBAction)stepperDidChange:(RPVerticalStepper *)stepper { self.stepperLabel.text = [NSString stringWithFormat:@"%.f", stepper.value]; } @end
Essentially, this code is free to use in commercial and non-commercial projects with no attribution necessary.
See the LICENSE file for more details.
Inspiration for this control from: PAStepper