我们经常看到购物车选购数量的时候可以加减,可以使用系统的框架UIStepper,也可以自己写,两种方法都很简单
方法一:(UIStepper)
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIStepper*stepper = [[UIStepperalloc]init];
stepper.minimumValue=0;//最大值
stepper.maximumValue=5;//最小值
stepper.stepValue=1;//每次增减值
stepper.value=0;//当前值
stepper.continuous=YES;//是否循环,默认循环
stepper.wraps=YES;//判断是否循环,即到最大值后又从最小值开始
stepper.tintColor= [UIColorpurpleColor];//设置按钮的颜色
[steppersetBackgroundColor:[UIColorwhiteColor]];//设置背景颜色
stepper.center=CGPointMake(160,240);
[stepperaddTarget:selfaction:@selector(valueChanged:)forControlEvents:(UIControlEventValueChanged)];
[self.viewaddSubview:stepper];
self.label= [[UILabelalloc]init];
self.label.frame=CGRectMake(150,300,40,30);
self.label.backgroundColor= [UIColorredColor];
[self.viewaddSubview:self.label];
}
- (void)valueChanged:(UIStepper*)stepper
{
[self.viewviewWithTag:200].alpha= stepper.value;
self.label.text= [NSStringstringWithFormat:@"%.2f", stepper.value];
NSLog(@"%f", stepper.value);
}
方法二:
自定义两个Button,实现方法是自增自减
- (void)viewDidLoad {
[superviewDidLoad];
self.title=@"小测试";
self.view.backgroundColor= [UIColorwhiteColor];
self.numberChange=0;
self.minusButton= [UIButtonbuttonWithType:UIButtonTypeSystem];
self.minusButton.frame=CGRectMake(100,120,30,30);
[self.minusButtonsetTitle:@"-"forState:UIControlStateNormal];
self.minusButton.backgroundColor= [UIColorgrayColor];
self.minusButton.layer.cornerRadius=self.minusButton.frame.size.width/2;
self.minusButton.layer.masksToBounds=YES;
self.minusButton.clipsToBounds=YES;
[self.minusButtonsetTitleColor:[UIColorwhiteColor]forState:UIControlStateNormal];
[self.minusButtonaddTarget:selfaction:@selector(minusNumber:)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:self.minusButton];
self.numberLabel= [[UILabelalloc]initWithFrame:CGRectMake(self.minusButton.frame.origin.x+self.minusButton.frame.size.width+5,self.minusButton.frame.origin.y,self.minusButton.frame.size.width,self.minusButton.frame.size.height)];
self.numberLabel.textAlignment=NSTextAlignmentCenter;
self.numberLabel.textColor= [UIColorlightGrayColor];
self.numberLabel.font= [UIFontsystemFontOfSize:14.0];
self.numberLabel.text= [NSStringstringWithFormat:@"%ld",self.numberChange];
[self.viewaddSubview:self.numberLabel];
self.addButton= [UIButtonbuttonWithType:UIButtonTypeSystem];
self.addButton.frame=CGRectMake(self.numberLabel.frame.origin.x+self.numberLabel.frame.size.width+5,self.numberLabel.frame.origin.y,self.numberLabel.frame.size.width,self.numberLabel.frame.size.height);
[self.addButtonsetTitle:@"+"forState:UIControlStateNormal];
[self.addButtonsetTitleColor:[UIColorwhiteColor]forState:UIControlStateNormal];
self.addButton.layer.cornerRadius=self.minusButton.frame.size.width/2;
self.addButton.backgroundColor= [UIColorgrayColor];
[self.addButtonaddTarget:selfaction:@selector(addNumber:)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:self.addButton];
}
#pragma mark -- UIButtonAction
//减数字
- (void)minusNumber:(UIButton*)button
{
if(self.numberChange==0) {
return;
}else{
self.numberChange--;
self.numberLabel.text= [NSStringstringWithFormat:@"%ld",self.numberChange];
}
}
//加数字
- (void)addNumber:(UIButton*)button{
if(self.numberChange>=10) {
return;
}else{
self.numberChange++;
self.numberLabel.text= [NSStringstringWithFormat:@"%ld",self.numberChange];
}
}