在iOS5中增加了一个UIStepper的新控件,UIStepper可以连续增加或减少一个数值。控件的外观是两个水平并排的按钮构成,一个显示为“+”,一个显示为“-”。如下图所示:
该控件一个有趣的特征是当用户按住“+”“-”按钮时,根据按住的时间长度,控件值的数字也以不同的数字改变。按住的时间越长,数值改变的越快。可以为UIStepper设定一个数值范围,比如0-99。
下面是UIStepper应用范例代码:
// Create a label to show the value in the stepper label = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, 100, 30)]; [label setTextColor:[UIColor whiteColor]]; [label setBackgroundColor:[UIColor clearColor]]; [label setTextAlignment:UITextAlignmentLeft]; [label setText: @"Quantity:"]; [[self view] addSubview:label]; // Frame defines location, size values are ignored UIStepper *stepper = [[UIStepper alloc] initWithFrame:CGRectMake(120, 20, 0, 0)]; // Set action target and action for a particular value changed event [stepper addTarget:self action:@selector(stepperPressed:) forControlEvents:UIControlEventValueChanged]; // Set min and max [stepper setMinimumValue:0]; [stepper setMaximumValue:99]; // Value wraps around from minimum to maximum [stepper setWraps:YES]; // If continuos (default), changes are sent for each change in stepper, // otherwise, change event occurs once user lets up on button [stepper setContinuous:NO]; // To change the increment value for each step // (default is 1) [stepper setStepValue:10];