PPNumberButton.m 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. //
  2. // PPNumberButton.m
  3. // PPNumberButton
  4. //
  5. // Created by AndyPang on 16/8/31.
  6. // Copyright © 2016年 AndyPang. All rights reserved.
  7. //
  8. #import "PPNumberButton.h"
  9. #ifdef DEBUG
  10. #define PPLog(...) NSLog(@"%s 第%d行 \n %@\n\n",__func__,__LINE__,[NSString stringWithFormat:__VA_ARGS__])
  11. #else
  12. #define PPLog(...)
  13. #endif
  14. @interface PPNumberButton ()<UITextFieldDelegate>
  15. /** 减按钮*/
  16. @property (nonatomic, strong) UIButton *decreaseBtn;
  17. /** 加按钮*/
  18. @property (nonatomic, strong) UIButton *increaseBtn;
  19. /** 数量展示/输入框*/
  20. @property (nonatomic, strong) UITextField *textField;
  21. /** 快速加减定时器*/
  22. @property (nonatomic, strong) NSTimer *timer;
  23. @end
  24. @implementation PPNumberButton
  25. - (instancetype)initWithFrame:(CGRect)frame
  26. {
  27. if (self = [super initWithFrame:frame])
  28. {
  29. [self setupUI];
  30. self.frame = frame;
  31. }
  32. return self;
  33. }
  34. - (instancetype)init
  35. {
  36. if (self = [super init])
  37. {
  38. [self setupUI];
  39. }
  40. return self;
  41. }
  42. - (void)awakeFromNib
  43. {
  44. [self setupUI];
  45. }
  46. #pragma mark - UI布局
  47. - (void)setupUI
  48. {
  49. self.backgroundColor = [UIColor whiteColor];
  50. self.layer.cornerRadius = 3.f;
  51. self.clipsToBounds = YES;
  52. //整个控件的默认尺寸(和某宝上面的按钮同样大小)
  53. self.frame = CGRectMake(0, 0, 110, 30);
  54. //减,加按钮
  55. _decreaseBtn = [self setupButtonWithTitle:@"-"];
  56. _increaseBtn = [self setupButtonWithTitle:@"+"];
  57. //数量展示/输入框
  58. _textField = [[UITextField alloc] init];
  59. _textField.text = @"1";
  60. _textField.delegate = self;
  61. _textField.font = [UIFont boldSystemFontOfSize:15];
  62. _textField.keyboardType = UIKeyboardTypeNumberPad;
  63. _textField.textAlignment = NSTextAlignmentCenter;
  64. _textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
  65. _textField.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
  66. [self addSubview:_textField];
  67. [self setFrame:self.frame];
  68. }
  69. //设置控件Frame
  70. - (void)setFrame:(CGRect)frame
  71. {
  72. [super setFrame:frame];
  73. //加减按钮为正方形
  74. CGFloat height = self.frame.size.height;
  75. CGFloat width = self.frame.size.width;
  76. _decreaseBtn.frame = CGRectMake(0, 0, height, height);
  77. _increaseBtn.frame = CGRectMake(width - height, 0, height, height);
  78. _textField.frame = CGRectMake(height, 0, width - height * 2, height);
  79. }
  80. //设置加减按钮的公共方法
  81. - (UIButton *)setupButtonWithTitle:(NSString *)text
  82. {
  83. UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
  84. button.titleLabel.font = [UIFont boldSystemFontOfSize:17];
  85. [button setTitle:text forState:UIControlStateNormal];
  86. [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
  87. [button addTarget:self action:@selector(touchDown:) forControlEvents:UIControlEventTouchDown];
  88. [button addTarget:self action:@selector(touchUp:) forControlEvents:UIControlEventTouchUpOutside|UIControlEventTouchUpInside|UIControlEventTouchCancel];
  89. [self addSubview:button];
  90. return button;
  91. }
  92. #pragma mark - UITextFieldDelegate
  93. - (void)textFieldDidEndEditing:(UITextField *)textField
  94. {
  95. textField.text.length == 0 || textField.text.integerValue <= 0 ? _textField.text = @"1" : nil;
  96. _numberBlock ? _numberBlock(_textField.text) : nil;
  97. _delegate ? [_delegate PPNumberButton:self number:_textField.text] : nil;
  98. }
  99. #pragma mark - 加减按钮点击响应
  100. //单击逐次加减,长按连续加减
  101. - (void)touchDown:(UIButton *)sender
  102. {
  103. [_textField resignFirstResponder];
  104. if (sender == _increaseBtn) {
  105. _timer = [NSTimer scheduledTimerWithTimeInterval:0.15 target:self selector:@selector(increase) userInfo:nil repeats:YES];
  106. } else {
  107. _timer = [NSTimer scheduledTimerWithTimeInterval:0.15 target:self selector:@selector(decrease) userInfo:nil repeats:YES];
  108. }
  109. [_timer fire];
  110. }
  111. - (void)touchUp:(UIButton *)sender
  112. {
  113. [self cleanTimer];
  114. }
  115. //加
  116. - (void)increase
  117. {
  118. _textField.text.length == 0 ? _textField.text = @"1" : nil;
  119. NSInteger number = [_textField.text integerValue] + 1;
  120. _textField.text = [NSString stringWithFormat:@"%ld", number];
  121. _numberBlock ? _numberBlock(_textField.text) : nil;
  122. _delegate ? [_delegate PPNumberButton:self number:_textField.text] : nil;
  123. }
  124. //减
  125. - (void)decrease
  126. {
  127. _textField.text.length == 0 ? _textField.text = @"1" : nil;
  128. NSInteger number = [_textField.text integerValue] - 1;
  129. if (number > 0)
  130. {
  131. _textField.text = [NSString stringWithFormat:@"%ld", number];
  132. _numberBlock ? _numberBlock(_textField.text) : nil;
  133. _delegate ? [_delegate PPNumberButton:self number:_textField.text] : nil;
  134. }
  135. else
  136. {
  137. _shakeAnimation ? [self shakeAnimation] : nil;
  138. NSLog(@"数量不能小于1");
  139. }
  140. }
  141. - (void)cleanTimer
  142. {
  143. if (_timer.isValid)
  144. {
  145. [_timer invalidate];
  146. _timer = nil;
  147. }
  148. }
  149. - (void)dealloc
  150. {
  151. [self cleanTimer];
  152. }
  153. #pragma mark - 加减按钮的属性设置
  154. - (void)setBorderColor:(UIColor *)borderColor
  155. {
  156. self.layer.borderColor = [borderColor CGColor];
  157. _decreaseBtn.layer.borderColor = [borderColor CGColor];
  158. _increaseBtn.layer.borderColor = [borderColor CGColor];
  159. self.layer.borderWidth = 0.5;
  160. _decreaseBtn.layer.borderWidth = 0.5;
  161. _increaseBtn.layer.borderWidth = 0.5;
  162. }
  163. - (void)setFont:(UIFont *)font
  164. {
  165. _increaseBtn.titleLabel.font = font;
  166. _decreaseBtn.titleLabel.font = font;
  167. }
  168. - (void)setTitleWithIncreaseTitle:(NSString *)increaseTitle decreaseTitle:(NSString *)decreaseTitle
  169. {
  170. [_increaseBtn setBackgroundImage:nil forState:UIControlStateNormal];
  171. [_decreaseBtn setBackgroundImage:nil forState:UIControlStateNormal];
  172. [_increaseBtn setTitle:increaseTitle forState:UIControlStateNormal];
  173. [_decreaseBtn setTitle:decreaseTitle forState:UIControlStateNormal];
  174. }
  175. - (void)setImageWithincreaseImage:(UIImage *)increaseImage decreaseImage:(UIImage *)decreaseImage
  176. {
  177. [_increaseBtn setTitle:@"" forState:UIControlStateNormal];
  178. [_decreaseBtn setTitle:@"" forState:UIControlStateNormal];
  179. [_increaseBtn setBackgroundImage:increaseImage forState:UIControlStateNormal];
  180. [_decreaseBtn setBackgroundImage:decreaseImage forState:UIControlStateNormal];
  181. }
  182. #pragma mark - 抖动动画
  183. - (void)shakeAnimation
  184. {
  185. CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position.x"];
  186. //获取当前View的position坐标
  187. CGFloat positionX = self.layer.position.x;
  188. //设置抖动的范围
  189. animation.values = @[@(positionX-10),@(positionX),@(positionX+10)];
  190. //动画重复的次数
  191. animation.repeatCount = 3;
  192. //动画时间
  193. animation.duration = 0.07;
  194. //设置自动反转
  195. animation.autoreverses = YES;
  196. [self.layer addAnimation:animation forKey:nil];
  197. }
  198. @end