SoFunction
Updated on 2025-04-03

UIStepper numerical adder and subtracter usage guide in iOS

UIStepper can continuously increase or decrease a value. The appearance of the control is composed of two horizontally side by side buttons, one displayed as "+" and the other displayed as "-".
An interesting feature of this control is that when the user holds the "+" and "-" buttons, the number of space values ​​also changes with different numbers depending on the length of time it is held. The longer the time you hold it, the faster the value changes. You can set a numerical range for UIStepper, such as 0-99. Its display effect is as follows:

1. Attribute description
value: The value currently represented, default is 0.0;
minimumValue: The minimum value that can be represented, default 0.0;
maximumValue: The maximum value that can be represented, default is 100.0;
stepValue: The value of each increment or decrement, default is 1.0;

2. How to judge whether to add ("+") or subtract ("-")
(1) Set a double* previousValue;   *// * to record the last value of *
(2) After operating the object you want to operate, turn = 0

Copy the codeThe code is as follows:

#pragma mark - Set UIStepper
- (void)createUIStepper{

    UIStepper * stepperButton = [[UIStepper alloc]initWithFrame:CGRectMake(225, 500, 30, 10)];
    [stepperButton addTarget:self action:@selector(controlStepperValue:) forControlEvents:UIControlEventValueChanged];
    = 100.0;
    = 0.0;
    = INITUISTEPPERVALUE;
    = 1.0;
    = YES;
    = NO;
    = YES;
    [ addSubview:stepperButton];
    [stepperButton release];

}


Copy the codeThe code is as follows:

- (void)controlStepperValue:(UIStepper *)stepper{

    if (_segment.selectedSegmentIndex == 0) {
        if ( > previousValue) {
            CGRect redRect = _redView.frame;
            += 5;
            _redView.frame = redRect;
        } else {

            CGRect redRect = _redView.frame;
            -= 5;
            _redView.frame = redRect;
        }
        previousValue = ;
    }else{
        if ( > previousValue) {
            CGRect redRect = _greenView.frame;
            += 5;
            _greenView.frame = redRect;
        } else {

            CGRect redRect = _greenView.frame;
            -= 5;
            _greenView.frame = redRect;
        }
        previousValue = ;
    }

}

3. Basic usage sorting
Initialize the control

Copy the codeThe code is as follows:

UIStepper * step = [[UIStepper alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];

Set whether the controller value triggers changes continuously
Copy the codeThe code is as follows:

@property(nonatomic,getter=isContinuous) BOOL continuous;

If set to YES, long pressing will trigger changes continuously. If set to NO, it will only be triggered after the press is finished.
Set whether long presses and changes are triggered
Copy the codeThe code is as follows:

@property(nonatomic) BOOL autorepeat;

If set to YES, the long pressing value will change continuously. If set to NO, the value will only be changed once a click.
Set whether the controller's value is cyclic (after reaching the boundary, start over, default is NO)
Copy the codeThe code is as follows:

@property(nonatomic) BOOL wraps;

Set the value of the controller
Copy the codeThe code is as follows:

@property(nonatomic) double value;

Set the maximum and minimum values ​​of the controller
Copy the codeThe code is as follows:

@property(nonatomic) double minimumValue;//Default is 0
@property(nonatomic) double maximumValue; //Default is 100

Set the step size of the controller
Copy the codeThe code is as follows:

@property(nonatomic) double stepValue;

Set controller style color
Copy the codeThe code is as follows:

@property(nonatomic,retain) UIColor *tintColor;

Set controller background picture
Copy the codeThe code is as follows:

- (void)setBackgroundImage:(UIImage*)image forState:(UIControlState)state;

Get background image
Copy the codeThe code is as follows:

- (UIImage*)backgroundImageForState:(UIControlState)state;

Pictures of setting the split line through the status of left and right buttons
Copy the codeThe code is as follows:

- (void)setDividerImage:(UIImage*)image forLeftSegmentState:(UIControlState)leftState rightSegmentState:(UIControlState)rightState;

Get the split line picture
Copy the codeThe code is as follows:

- (UIImage*)dividerImageForLeftSegmentState:(UIControlState)state rightSegmentState:(UIControlState)state;

Set and get pictures of plus buttons
Copy the codeThe code is as follows:

- (void)setIncrementImage:(UIImage *)image forState:(UIControlState)state;
- (UIImage *)incrementImageForState:(UIControlState)state;

Setting and getting pictures of minus buttons
Copy the codeThe code is as follows:

- (void)setDecrementImage:(UIImage *)image forState:(UIControlState)state;
- (UIImage *)decrementImageForState:(UIControlState)state;