UIStepper numerical Adder in iOS

  • 2020-06-12 10:42:32
  • OfStack

UIStepper can continuously increase or decrease 1 value. The appearance of the control is made up of two horizontal buttons, one displayed as a "+" and one as a "-".
An interesting feature of this control is that when the user holds down the "+" and "-" buttons, the number of the space value changes with different Numbers depending on the length of time the user holds down. The longer 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: Minimum representable value, default 0.0;
maximumValue: Maximum representable value, default 100.0;
stepValue: The value of each increment or decrement, default is 1.0;

2. How to judge plus ("+") and minus ("-")
(1) Set 1 double* previousValue; *// * is used to record the previous value of Stepper. value*
(2) Stepper. value = 0 after operating on the object you want to operate


#pragma mark - Set up the UIStepper
- (void)createUIStepper{     UIStepper * stepperButton = [[UIStepper alloc]initWithFrame:CGRectMake(225, 500, 30, 10)];
    [stepperButton addTarget:self action:@selector(controlStepperValue:) forControlEvents:UIControlEventValueChanged];
    stepperButton.maximumValue = 100.0;
    stepperButton.minimumValue = 0.0;
    stepperButton.value = INITUISTEPPERVALUE;
    stepperButton.stepValue = 1.0;
    stepperButton.continuous = YES;
    stepperButton.wraps = NO;
    stepperButton.autorepeat = YES;
    [self.view addSubview:stepperButton];
    [stepperButton release]; }


- (void)controlStepperValue:(UIStepper *)stepper{     if (_segment.selectedSegmentIndex == 0) {
        if (stepper.value > previousValue) {
            CGRect redRect = _redView.frame;
            redRect.size.height += 5;
            _redView.frame = redRect;
        } else {             CGRect redRect = _redView.frame;
            redRect.size.height -= 5;
            _redView.frame = redRect;
        }
        previousValue = stepper.value;
    }else{
        if (stepper.value > previousValue) {
            CGRect redRect = _greenView.frame;
            redRect.size.height += 5;
            _greenView.frame = redRect;
        } else {             CGRect redRect = _greenView.frame;
            redRect.size.height -= 5;
            _greenView.frame = redRect;
        }
        previousValue = stepper.value;
    } }

3. Organize basic usage
Initialization control unit


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

Sets whether controller values trigger changes continuously

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

If set to YES, the long press will trigger the change continuously. If set to NO, it will trigger only after the click.
Set long press whether 1 straight trigger change

@property(nonatomic) BOOL autorepeat;

If set to YES, the long press value will change 1 straight; if set to NO, one click will change the value only 1 time
Sets the value of the controller to loop (after reaching the boundary, start again, NO by default)

@property(nonatomic) BOOL wraps;

Sets the value of the controller

@property(nonatomic) double value;

Sets the maximum and minimum values for the controller

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

Set the step size of the controller

@property(nonatomic) double stepValue;

Set the controller style color

@property(nonatomic,retain) UIColor *tintColor;

Set the controller background image

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

Get the background image

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

Set the image of the divider by the state of the left and right buttons

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

Gets the secant line image

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

Sets and gets the picture of the plus button

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

Sets and gets the picture of the minus button

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