iOS中的UIStepper数值加减器用法指南
投稿:goldensun
UIStepper可以连续增加或减少一个数值。控件的外观是两个水平并排的按钮构成,一个显示为“+”,一个显示为“-”。
该控件的一个有趣的特征是当用户按住“+”,“-”按钮时,根据按住的时间长度,空间值的数字也以不同的数字改变。按住的时间越长,数值改变的越快。可以为UIStepper设定一个数值范围,比如0-99. 它的显示效果如下:
1. 属性说明
value: 当前所表示的值,默认为0.0;
minimumValue: 最小可以表示的值,默认0.0;
maximumValue: 最大可以表示的值,默认100.0;
stepValue: 每次递增或递减的值,默认为1.0;
2.如何判断加("+")减("-")
(1)通过设置一个 double* previousValue; *// *用来记录Stepper.value*的上一次值
(2)在对想操作的对象进行操作后,将Stepper.value = 0
#pragma mark - 设置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.基本用法整理
初始化控件
UIStepper * step = [[UIStepper alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
设置控制器值是否连续触发变化
@property(nonatomic,getter=isContinuous) BOOL continuous;
若设置为YES,则长按会连续触发变化,若设置为NO,只有在按击结束后,才会触发。
设置长按是否一直触发变化
@property(nonatomic) BOOL autorepeat;
若设置为YES,则长按值会一直改变,若设置为NO,则一次点击只会改变一次值
设置控制器的值是否循环(到达边界后,重头开始,默认为NO)
@property(nonatomic) BOOL wraps;
设置控制器的值
@property(nonatomic) double value;
设置控制器的最大值和最小值
@property(nonatomic) double minimumValue;//默认为0
@property(nonatomic) double maximumValue; //默认为100
设置控制器的步长
@property(nonatomic) double stepValue;
设置控制器风格颜色
@property(nonatomic,retain) UIColor *tintColor;
设置控制器背景图片
- (void)setBackgroundImage:(UIImage*)image forState:(UIControlState)state;
获取背景图片
- (UIImage*)backgroundImageForState:(UIControlState)state;
通过左右按钮的状态设置分割线的图片
- (void)setDividerImage:(UIImage*)image forLeftSegmentState:(UIControlState)leftState rightSegmentState:(UIControlState)rightState;
获取分割线图片
- (UIImage*)dividerImageForLeftSegmentState:(UIControlState)state rightSegmentState:(UIControlState)state;
设置和获取加号按钮的图片
- (void)setIncrementImage:(UIImage *)image forState:(UIControlState)state;
- (UIImage *)incrementImageForState:(UIControlState)state;
设置和获取减号按钮的图片
- (void)setDecrementImage:(UIImage *)image forState:(UIControlState)state;
- (UIImage *)decrementImageForState:(UIControlState)state;