我们先来看看苹果自带的UIStepper的效果图:
可以发现中间并没有数字在中间变化,而实际项目中原型图是这样的(红色圈中的):
他的功能是:
点击左边的可以减,点击右边加的时候,可以加,中间的数字会随着变化
暂没实现连续加减的功能,其实也挺简单的,给他们加个长按手势处理一下就可以了,因为项目没有这个要求就没做了,
下面是参考代码,欢迎批评,指正!
#import "priceView.h"
@implementation priceView
-(instancetype)initWithFrame:(CGRect)frame
{
if (self=[super initWithFrame:frame]) {
[self initSubviews];
}
return self;
}
-(void)initSubviews
{
[self addSubview:self.minusImageView];
[self addSubview:self.plusImageView];
[self addSubview:self.numberLabel];
[self.minusImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.mas_top).offset(0);
make.left.equalTo(self.mas_left).offset(0);
make.bottom.equalTo(self.mas_bottom).offset(0);
make.width.equalTo(@21);
}];
[self.plusImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.mas_top).offset(0);
make.right.equalTo(self.mas_right).offset(0);
make.bottom.equalTo(self.mas_bottom).offset(0);
make.width.equalTo(@21);
}];
[self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.mas_top).offset(0);
make.right.equalTo(self.plusImageView.mas_left).offset(0);
make.bottom.equalTo(self.mas_bottom).offset(0);
make.left.equalTo(self.minusImageView.mas_right).offset(0);
}];
}
-(UIImageView*)minusImageView
{
if (_minusImageView==nil) {
_minusImageView=[[UIImageView alloc]init];
_minusImageView.userInteractionEnabled=YES;
_minusImageView.contentMode=UIViewContentModeScaleAspectFit;
UITapGestureRecognizer* tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(minusTapAction)];
[_minusImageView addGestureRecognizer:tap];
_minusImageView.image=[UIImage imageNamed:@"minus-sign"];
}
return _minusImageView;
}
#pragma mark - 减号响应方法
-(void)minusTapAction
{
if (_number==0)
{
[MBProgressHUD showMessage:@"价格不能为负哦,亲"];
}
else
{
_number--;
[self updateNumberlabel];
}
}
-(UIImageView*)plusImageView
{
if (_plusImageView==nil) {
_plusImageView=[[UIImageView alloc]init];
_plusImageView.userInteractionEnabled=YES;
_plusImageView.contentMode=UIViewContentModeScaleAspectFit;
UITapGestureRecognizer* tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(plusTapAction)];
[_plusImageView addGestureRecognizer:tap];
_plusImageView.image=[UIImage imageNamed:@"plus"];
}
return _plusImageView;
}
#pragma mark - 加号响应方法
-(void)plusTapAction
{
_number++;
[self updateNumberlabel];
}
-(void)updateNumberlabel
{
_numberLabel.text=[NSString stringWithFormat:@"%zd.0",_number];
}
-(UILabel*)numberLabel{
if (_numberLabel==nil) {
_numberLabel=[[UILabel alloc]init];
_numberLabel.text=@"0.0";
_numberLabel.textAlignment=NSTextAlignmentCenter;
_numberLabel.textColor=[UIColor blackColor];
_numberLabel.font =[UIFont boldSystemFontOfSize:35.0];
}
return _numberLabel;
}
@end
注意!使用时同样需导入marsonry框架!