购物车商品加减操作
程序员文章站
2022-05-22 19:55:58
...
XXNumberButton 针对购物车商品进行加减操作,输入框是动态改变大小的,可以修改输入框字体颜色,加减按钮文字背景图,都是单独拿出来可以设置信息,可以设置TextField是否可输入。
//
// XXNumberButton.h
//
// Created by Mac on 2018/8/28.
// Copyright © 2018年 xiangxx. All rights reserved.
//
#import <UIKit/UIKit.h>
typedef void(^BlockCurrentNumber)(NSString *currentNumber);
@interface XXNumberButton : UIView
- (instancetype)initWithFrame:(CGRect)frame;
@property (nonatomic, copy) BlockCurrentNumber blockCurrentNumber;
/** 加按钮背景图片 */
@property (nonatomic, strong ) IBInspectable UIImage *increaseImage;
/** 减按钮背景图片 */
@property (nonatomic, strong ) IBInspectable UIImage *decreaseImage;
/** 加按钮标题 */
@property (nonatomic, copy ) IBInspectable NSString *increaseTitle;
/** 减按钮标题 */
@property (nonatomic, copy ) IBInspectable NSString *decreaseTitle;
/** 加减按钮的字体大小 */
@property (nonatomic, assign ) IBInspectable CGFloat buttonTitleFont;
/** 输入框中的字体大小 */
@property (nonatomic, assign ) IBInspectable CGFloat inputFieldFont;
/** 输入框中的字体颜色 */
@property (nonatomic, strong) IBInspectable UIColor *inputFieldColor;
/** 最小值, default is 0 */
@property (nonatomic, assign ) IBInspectable CGFloat minValue;
/** 最大值 */
@property (nonatomic, assign ) CGFloat maxValue;
/** 递增步长,默认步长为1 */
@property (nonatomic, assign ) CGFloat stepValue;
/** 为YES时,初始化时减号按钮隐藏(饿了么/百度外卖/美团外卖按钮模式), default is NO*/
@property (nonatomic, assign ) IBInspectable BOOL decreaseHide;
/** 是否可以使用键盘输入, default is YES*/
@property (nonatomic, assign, getter=isEditing) IBInspectable BOOL editing;
@end
#pragma mark - NSString分类
@interface NSString (XXNumberButton)
/**
字符串 nil, @"", @" ", @"\n" Returns NO;
其他 Returns YES.
*/
- (BOOL)isNotBlank;
@end
//
// NumberButton.m
// XXNumberButton
//
// Created by Mac on 2018/8/28.
// Copyright © 2018年 xiangxx. All rights reserved.
//
#import "XXNumberButton.h"
@interface XXNumberButton()<UITextFieldDelegate>
@property (nonatomic, assign) CGFloat width; // 控件自身的宽
@property (nonatomic, assign) CGFloat height; // 控件自身的高
// 减按钮
@property (nonatomic, strong) UIButton *decreaseBtn;
// 加按钮
@property (nonatomic, strong) UIButton *increaseBtn;
// 数量展开输入框
@property (nonatomic, strong) UITextField *textField;
@end
@implementation XXNumberButton
- (instancetype)initWithCoder:(NSCoder *)aDecoder{
self = [super initWithCoder:aDecoder];
if (self) {
[self initUI];
}
return self;
}
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
[self initUI];
if(CGRectIsEmpty(frame)) {self.frame = CGRectMake(0, 0, 110, 30);};
}
return self;
}
#pragma mark - layoutSubviews
- (void)layoutSubviews
{
[super layoutSubviews];
_width = self.frame.size.width;
_height = self.frame.size.height;
_textField.frame = CGRectMake(_height, 0, _width - 2*_height, _height);
_increaseBtn.frame = CGRectMake(_width - _height, 0, _height, _height);
if (_decreaseHide && _textField.text.floatValue < _minValue) {
_decreaseBtn.frame = CGRectMake(_width-_height, 0, _height, _height);
} else {
_decreaseBtn.frame = CGRectMake(0, 0, _height, _height);
}
}
// 初始化界面
- (void)initUI{
_buttonTitleFont = 17;
_inputFieldFont = 15;
_minValue = 0;
_maxValue = NSIntegerMax;
self.stepValue = 1;
_increaseBtn = [self createButton];
_decreaseBtn = [self createButton];
//数量展示/输入框
_textField = [[UITextField alloc] init];
_textField.delegate = self;
_textField.textAlignment = NSTextAlignmentCenter;
_textField.keyboardType = UIKeyboardTypeDecimalPad;
_textField.font = [UIFont systemFontOfSize:_inputFieldFont];
_textField.text = [NSString stringWithFormat:@"%.0f",_minValue];
_textField.textColor = [UIColor grayColor];
[self addSubview:_increaseBtn];
[self addSubview:_decreaseBtn];
[self addSubview:_textField];
self.clipsToBounds = YES;
self.layer.cornerRadius = 3.0f;
self.backgroundColor = [UIColor whiteColor];
}
// 设置加减 按钮的公共方法
- (UIButton *)createButton{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.titleLabel.font = [UIFont boldSystemFontOfSize:_buttonTitleFont];
[btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(touchDown:) forControlEvents:UIControlEventTouchUpInside];
return btn;
}
#pragma mark - 加减按钮点击响应
- (void)touchDown:(UIButton *)sender
{
[_textField resignFirstResponder];
if (sender == _increaseBtn) {
[self increase];
} else {
[self decrease];
}
}
// 加运算
- (void)increase{
CGFloat number = [_textField.text floatValue] + self.stepValue;
if (number <= _maxValue) {
// 当按钮为"减号按钮隐藏模式",且输入框值==设定最小值,减号按钮展开
if (_decreaseHide && number==_minValue) {
__weak typeof(self)weakSelf = self;
[UIView animateWithDuration:0.3f animations:^{
weakSelf.decreaseBtn.alpha = 1;
weakSelf.decreaseBtn.frame = CGRectMake(0, 0, weakSelf.height, weakSelf.height);
} completion:^(BOOL finished) {
weakSelf.textField.hidden = NO;
}];
}
_textField.text = [NSString stringWithFormat:@"%.0f",number];
if (self.blockCurrentNumber) {
self.blockCurrentNumber([NSString stringWithFormat:@"%@",_textField.text]);
}
} else {
if (self.blockCurrentNumber) {
self.blockCurrentNumber([NSString stringWithFormat:@"数据已超过最大数量%.0f",_maxValue]);
}
}
}
// 减运算
- (void)decrease{
CGFloat number = [_textField.text floatValue] - self.stepValue;
if (number >= _minValue) {
_textField.text = [NSString stringWithFormat:@"%.0f",number];
} else {
// 当按钮为"减号按钮隐藏模式",且输入框值 < 设定最小值,减号按钮隐藏
if (_decreaseHide && number<_minValue) {
_textField.hidden = YES;
_textField.text = [NSString stringWithFormat:@"%.0f",_minValue-1];
__weak typeof(self)weakSelf = self;
[UIView animateWithDuration:0.3f animations:^{
weakSelf.decreaseBtn.alpha = 0;
weakSelf.decreaseBtn.frame = CGRectMake(weakSelf.width - weakSelf.height, 0, weakSelf.height, weakSelf.height);
}];
return;
}else{
if (self.blockCurrentNumber) {
self.blockCurrentNumber([NSString stringWithFormat:@"数据不能小于最小值%.0f",_minValue]);
}
return;
}
}
if (self.blockCurrentNumber) {
self.blockCurrentNumber([NSString stringWithFormat:@"%@",_textField.text]);
}
}
#pragma mark - 加减按钮的属性设置 为YES时,初始化时减号按钮隐藏
- (void)setDecreaseHide:(BOOL)decreaseHide
{
// 当按钮为"减号按钮隐藏模式(饿了么/百度外卖/美团外卖按钮样式)"
if (decreaseHide) {
if (_textField.text.floatValue <= _minValue) {
_textField.hidden = YES;
_decreaseBtn.alpha = 0;
_textField.text = [NSString stringWithFormat:@"%.0f",_minValue-1];
_decreaseBtn.frame = CGRectMake(_width-_height, 0, _height, _height);
}
self.backgroundColor = [UIColor clearColor];
} else {
_decreaseBtn.frame = CGRectMake(0, 0, _height, _height);
}
_decreaseHide = decreaseHide;
}
#pragma mark set get 方法
- (void)setIncreaseImage:(UIImage *)increaseImage{
_increaseImage = increaseImage;
[_increaseBtn setBackgroundImage:increaseImage forState:UIControlStateNormal];
}
- (void)setDecreaseImage:(UIImage *)decreaseImage{
_decreaseImage = decreaseImage;
[_decreaseBtn setBackgroundImage:decreaseImage forState:UIControlStateNormal];
}
- (void)setIncreaseTitle:(NSString *)increaseTitle{
_increaseTitle = increaseTitle;
[_increaseBtn setTitle:increaseTitle forState:UIControlStateNormal];
}
- (void)setDecreaseTitle:(NSString *)decreaseTitle{
_decreaseTitle = decreaseTitle;
[_decreaseBtn setTitle:decreaseTitle forState:UIControlStateNormal];
}
- (void)setButtonTitleFont:(CGFloat)buttonTitleFont{
_buttonTitleFont = buttonTitleFont;
_increaseBtn.titleLabel.font = [UIFont systemFontOfSize:buttonTitleFont];
_decreaseBtn.titleLabel.font = [UIFont systemFontOfSize:buttonTitleFont];
}
- (void)setInputFieldFont:(CGFloat)inputFieldFont{
_inputFieldFont = inputFieldFont;
_textField.font = [UIFont systemFontOfSize:inputFieldFont];
}
- (void)setInputFieldColor:(UIColor *)inputFieldColor{
_inputFieldColor = inputFieldColor;
_textField.textColor = inputFieldColor;
}
- (void)setEditing:(BOOL)editing{
_editing = editing;
_textField.enabled = editing;
}
- (void)setMinValue:(CGFloat)minValue{
_minValue = minValue;
}
- (void)setMaxValue:(CGFloat)maxValue{
_maxValue = maxValue;
}
- (void)setStepValue:(CGFloat)stepValue{
_stepValue = stepValue;
}
#pragma mark TextFieldDelegate
- (void)textFieldDidEndEditing:(UITextField *)textField{
[self checkTextFieldNumberWithUpdate];
}
// 检查TextField中数字的合法性,并修正
- (void)checkTextFieldNumberWithUpdate
{
NSString *minValueString = [NSString stringWithFormat:@"%.0f",_minValue];
NSString *maxValueString = [NSString stringWithFormat:@"%.0f",_maxValue];
if ([_textField.text isNotBlank] == NO || [_textField.text floatValue] < _minValue) {
_textField.text = _decreaseHide ? [NSString stringWithFormat:@"%.0f",minValueString.floatValue-self.stepValue]:minValueString;
}
[_textField.text floatValue] > _maxValue ? _textField.text = maxValueString : nil;
[_textField.text floatValue] < _minValue ? _textField.text = minValueString : nil;
if (self.blockCurrentNumber) {
self.blockCurrentNumber([NSString stringWithFormat:@"%@",_textField.text]);
}
}
@end
#pragma mark - NSString分类
@implementation NSString (XXNumberButton)
//判断 字符串 nil, @"", @" ", @"\n" Returns NO;
- (BOOL)isNotBlank
{
NSCharacterSet *blank = [NSCharacterSet whitespaceAndNewlineCharacterSet];
for (NSInteger i = 0; i < self.length; ++i) {
unichar c = [self characterAtIndex:i];
if (![blank characterIsMember:c]) {
return YES;
}
}
return NO;
}
@end
点击进入下载代码地址