步进控件——UIStepper

  步进控件,可用于替换传统用于输入值的文本框。步进控件提供了“+”和“-”两个按钮,用来改变stepper内部value的增加或减少,调用的事件是UIControlEventValueChanged。由于它是不显示值的,所以它一般是和label配合使用。

  stepper常用的属性有:

  (1) value属性:stepper的当前值

  (2)minimumValue属性:stepper的最小值

  (3)maximumValue属性:stepper的最大值

  (4)stepValue属性:stepper每步的大小

 

  (5)continuous属性:按住按钮,连续跳动过程中,中间值是否显示。默认是YES

  (6)autorepeat属性:按住按钮,是否能连续跳动。默认是YES

  (7)wraps属性:stepper的值达到最小或最大值时,是否可循环。默认是NO

 

- (void)viewDidLoad {

    [super viewDidLoad];

    

    //创建一个label用来显示stepper的值

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 40)];

    label.tag = 111;

    label.textAlignment = NSTextAlignmentCenter;

    [self.view addSubview:label];

    

    //创建stepper控件,并指定它的位置

    UIStepper *stepper = [[UIStepper alloc] initWithFrame:CGRectMake(100, 150, 0, 0)];

    //指定stepper的最小值为0

    stepper.minimumValue = 0;

    //指定stepper的最大值为10

    stepper.maximumValue = 10;

    //指定stepper跳动的幅度为2

    stepper.stepValue = 2;

    //指定stepper的初始值为5

    stepper.value = 5;

    label.text = [NSString stringWithFormat:@"%f",stepper.value];

    

    //设定连续跳动过程中,中间值显示

    stepper.continuous = YES;

    //设定stepper能连续跳动

    stepper.autorepeat = YES;

    //设定stepper的值可循环

    stepper.wraps = YES;

    

    //当stepper的value改变时,调用方法。

    [stepper addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged];

    [self.view addSubview:stepper];

}

 

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

    UILabel *label = (UILabel*)[self.view viewWithTag:111];

    label.text = [NSString stringWithFormat:@"%f",stepper.value];

}

 

posted on 2015-08-11 15:37  乱七八糟21号  阅读(735)  评论(0编辑  收藏  举报

导航