UIStepper 微调器

外观和UISwitch相似 包含+ -按钮 控制某个值得增,减

UIControl基类

1. Value:

Minimum:最小值 默认为0 设为大于或等于maximumValue时,会报一个NSInvalidArgumentException异常

Maximum:最大值 默认为100 同会报异常

Current:UIStepper控件的当前值,对应于Value

Step:数值变化的补偿对应stepValue属性 默认为1

2.Behavior

Autorepeat(autorepeat):默认为YES-按住加号或减号不松手,数字会持续变化

Continuous(continuous):默认为YES-用户交互时会立即放松ValueChanged事件,NO则表示只有等用户交互结束时才放松ValueChanged事件

Wrap(wraps):默认为NO YES-若value加到超过maximumValue将自动转头编程minmumValue的值 到minimumValue亦会反转

定制外观:

setXxxImage:forState“

setDecrementImage: forState: 定义减号按钮的图片

setIncrementImage: forState: 定义加号按钮的图片

.h

@property (nonatomic,strong) UIStepper* stepper1;

@property (nonatomic,strong) UIStepper* stepper2;

@property (nonatomic,strong) UIStepper* stepper3;

@property (nonatomic,strong) UITextField* tf1;

@property (nonatomic,strong) UITextField* tf2;

@property (nonatomic,strong) UITextField* tf3;

.m

- (void)viewDidLoad {

[super viewDidLoad];

self.stepper1=[[UIStepper alloc]initWithFrame:CGRectMake(20, 40, 70, 40)];

self.stepper1.tag=1;

[self.stepper1 addTarget:self action:@selector(valuechanged:) forControlEvents:UIControlEventValueChanged];

[self.view addSubview:self.stepper1];

self.stepper2=[[UIStepper alloc]initWithFrame:CGRectMake(20, 70, 70, 40)];

[self.stepper2 setMinimumValue:5];

[self.stepper2 setMaximumValue:60];

[self.stepper2 setStepValue:5];

self.stepper2.autorepeat=NO;

self.stepper2.continuous=NO;

self.stepper2.tag=2;

[self.stepper2 addTarget:self action:@selector(valuechanged:) forControlEvents:UIControlEventValueChanged];

[self.view addSubview:self.stepper2];

self.stepper3=[[UIStepper alloc]initWithFrame:CGRectMake(20, 110, 70, 40)];

[self.stepper3 setMinimumValue:10];

[self.stepper3 setMaximumValue:50];

self.stepper3.tag=3;

self.stepper3.wraps=YES;

[self.stepper3 setDecrementImage:[UIImage imageNamed:@"minus.gif"] forState:UIControlStateNormal];

[self.stepper3 setIncrementImage:[UIImage imageNamed:@"plus.gif"] forState:UIControlStateNormal];

[self.stepper3 addTarget:self action:@selector(valuechanged:) forControlEvents:UIControlEventValueChanged];

[self.view addSubview:self.stepper3];

self.tf1=[[UITextField alloc]initWithFrame:CGRectMake(170, 40, 120, 40)];

[self.view addSubview:self.tf1];

self.tf2=[[UITextField alloc]initWithFrame:CGRectMake(170, 70, 120, 40)];

[self.view addSubview:self.tf2];

self.tf3=[[UITextField alloc]initWithFrame:CGRectMake(170, 110, 120, 40)];

[self.view addSubview:self.tf3];

// Do any additional setup after loading the view, typically from a nib.

}

-(void)valuechanged:(id)sender{

switch ([sender tag]) {

case 1:

self.tf1.text=[NSString stringWithFormat:@"%f", self.stepper1.value];

break;

case 2:

self.tf2.text=[NSString stringWithFormat:@"%f", self.stepper2.value];

break;

case 3:

self.tf3.text=[NSString stringWithFormat:@"%f", self.stepper3.value];

break;

default:

break;

}

}

你可能感兴趣的:(UIStepper 微调器)