購物車商品加減操作
阿新 • • 發佈:2018-11-07
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
點選進入下載程式碼地址