@在IOS5中增加了一个UIStepper的新控件,UIStepper可以连续增加或减少一个数值。控件的外观是两个水平并排的按钮构成,一个显示为“+”,一个显示为“-”。
1. 初始化控件
UIStepper *_stepper = [[[UIStepper alloc] initWithFrame:CGRectMake(120, 20, 0, 0)] autorelease];
// 响应事件
[_stepper addTarget:self action:@selector(updateValue:) forControlEvents:UIControlEventValueChanged];
//设置最大值和最小值
[_stepper setMinimumValue:0];
[_stepper setMaximumValue:100];
// 控制值是否在[minimumValue, maximumValue]区间内循环,默认NO。
[_stepper setWraps:YES];
// 控制是否持续触发UIControlEventValueChanged事件。默认为YES,即当按住时每次值改变都触发一次UIControlEventValueChanged事件,否则只有在释放时触发UIControlEventValueChanged事件。
_stepper. continuous = YES;
// 每次递增或递减的值
[_stepper setStepValue:10];
// 控制是否在按住时自动持续递增或递减,默认YES;
_stepper.autorepeat = YES;
2. 属性说明
value: 当前所表示的值,默认为0.0;
minimumValue: 最小可以表示的值,默认0.0;
maximumValue: 最大可以表示的值,默认100.0;
stepValue: 每次递增或递减的值,默认为1.0;
3.如何判断加("+")减("-")
(1)通过设置一个 double previousValue; // 用来记录 Stepper.value 的上一次值
(2)在对想操作的对象进行操作后,将Stepper.value = 0
- #pragma mark - 设置UIStepper
- - ( void )createUIStepper{
- UIStepper * stepperButton = [[UIStepper alloc ]initWithFrame:CGRectMake(2 2 5 , 5 0 0 , 3 0 , 1 0 )];
- [stepperButton addTarget : self action : @selector (controlStepperValue:) forControlEvents :UIControlEventValueChanged];
- stepperButton .maximumValue = 1 0 0 .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 ;
- }
- }
//如下面例子
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor=[UIColor yellowColor];
UIStepper *stepper = [[UIStepper alloc] init];
stepper.tag = 10;
stepper.center = CGPointMake(160, 240);
stepper.minimumValue = 1; //设置最小值
stepper.maximumValue = 30; //设置最大值
stepper.stepValue = 2; //每次递增2
stepper.value = 15; //初始值
[stepper setWraps:YES]; //是否循环
[stepper addTarget:self action:@selector(doTest) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:stepper];
[stepper release];
}
-(void)doTest
{
UIStepper *per = (UIStepper*)[self.view viewWithTag:10];
if (per.continuous)
{
NSLog(@"Y");
int a = per.value; //获取当前值
NSLog(@"%d", a);
}
else
{
NSLog(@"N");
}
}