Açıklama Yok

FKSelectSpecAmountView.m 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //
  2. // FKSelectSpecAmountView.m
  3. // FirstLink
  4. //
  5. // Created by jack on 16/1/18.
  6. // Copyright © 2016年 FirstLink. All rights reserved.
  7. //
  8. #import "FKSelectSpecAmountView.h"
  9. @interface FKSelectSpecAmountView ()
  10. @property (nonatomic, strong) UIButton *addBtn;
  11. @property (nonatomic, strong) UIButton *reductBtn;
  12. @property (nonatomic, strong) UILabel *numberLabel;
  13. @end
  14. @implementation FKSelectSpecAmountView
  15. - (instancetype)initWithFrame:(CGRect)frame{
  16. if (self = [super initWithFrame:frame]) {
  17. [self addAllSubviews];
  18. }
  19. return self;
  20. }
  21. - (void)configWithSpecDataCenter:(FKProductSpecDataCenter *)dataCenter{
  22. NSUInteger stockCount = [dataCenter getTotalStock];
  23. FKProductItem *targetItem = [dataCenter getSelectedProductItem];
  24. if (targetItem) {
  25. stockCount = [targetItem realStockCount];
  26. }
  27. if (stockCount <= 10){
  28. self.remainLabel.hidden = NO;
  29. self.remainLabel.text = [NSString stringWithFormat:@"(剩余%lu件)", (unsigned long)stockCount];
  30. if (stockCount <= 0) self.remainLabel.text = @"缺货";
  31. } else{
  32. self.remainLabel.hidden = YES;
  33. }
  34. }
  35. #pragma mark - Layout
  36. - (void)addAllSubviews{
  37. [self addSubview:self.numberLabel];
  38. [self addSubview:self.addBtn];
  39. [self addSubview:self.reductBtn];
  40. [self addSubview:self.amountLabel];
  41. [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  42. make.left.equalTo(self).offset(15);
  43. make.centerY.equalTo(self);
  44. }];
  45. [self.addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  46. make.right.equalTo(self).offset(- 15);
  47. make.centerY.equalTo(self);
  48. make.width.height.mas_equalTo(27);
  49. }];
  50. [self.amountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  51. make.right.equalTo(self.addBtn.mas_left).offset(- 2);
  52. make.top.height.equalTo(self.addBtn);
  53. make.width.equalTo(@60);
  54. }];
  55. [self.reductBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  56. make.top.width.height.equalTo(self.addBtn);
  57. make.right.equalTo(self.amountLabel.mas_left).offset(- 2);
  58. }];
  59. [self addSubview:self.remainLabel];
  60. [self.remainLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  61. make.right.equalTo(self.reductBtn.mas_left).offset(-10);
  62. make.centerY.equalTo(self.reductBtn);
  63. }];
  64. }
  65. #pragma mark - propety
  66. - (void)clickAddBtn:(UIButton *)sender{
  67. NSInteger count = self.amountLabel.text.integerValue + 1;
  68. self.amountLabel.text = [NSString stringWithFormat:@"%ld", (long)count];
  69. }
  70. - (void)clickReduceBtn:(UIButton *)sender{
  71. NSInteger count = self.amountLabel.text.integerValue - 1;
  72. if (count <= 0) count = 1;
  73. self.amountLabel.text = [NSString stringWithFormat:@"%ld", (long)count];
  74. }
  75. #pragma mark - property
  76. - (UILabel *)amountLabel{
  77. if (_amountLabel == nil) {
  78. _amountLabel = [[UILabel alloc]init];
  79. _amountLabel.layer.borderWidth = 0.5f;
  80. _amountLabel.layer.borderColor = [UIColorFromRGB(0xcccccc) CGColor];
  81. _amountLabel.textAlignment = NSTextAlignmentCenter;
  82. _amountLabel.lineBreakMode = NSLineBreakByTruncatingTail;
  83. _amountLabel.numberOfLines = 1;
  84. _amountLabel.textColor = UIColorFromRGB(0x333333);
  85. _amountLabel.text = @"1";
  86. }
  87. return _amountLabel;
  88. }
  89. - (UILabel *)remainLabel {
  90. if (_remainLabel == nil) {
  91. _remainLabel = [[UILabel alloc] init];
  92. _remainLabel.font = [UIFont systemFontOfSize:12];
  93. _remainLabel.textAlignment = NSTextAlignmentCenter;
  94. _remainLabel.textColor = UIColorFromRGB(0xff6362);
  95. _remainLabel.hidden = YES;
  96. }
  97. return _remainLabel;
  98. }
  99. - (UIButton *)addBtn{
  100. if (_addBtn == nil) {
  101. _addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  102. [_addBtn setImage:[UIImage imageNamed:@"specification_body_add_n"] forState:UIControlStateNormal];
  103. [_addBtn addTarget:self action:@selector(clickAddBtn:) forControlEvents:UIControlEventTouchUpInside];
  104. }
  105. return _addBtn;
  106. }
  107. - (UIButton *)reductBtn{
  108. if (_reductBtn == nil) {
  109. _reductBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  110. [_reductBtn setImage:[UIImage imageNamed:@"specification_body_lower_n"] forState:UIControlStateNormal];
  111. [_reductBtn addTarget:self action:@selector(clickReduceBtn:) forControlEvents:UIControlEventTouchUpInside];
  112. }
  113. return _reductBtn;
  114. }
  115. - (UILabel *)numberLabel{
  116. if (_numberLabel == nil) {
  117. _numberLabel = [[UILabel alloc]init];
  118. _numberLabel.backgroundColor = [UIColor clearColor];
  119. _numberLabel.font = [UIFont systemFontOfSize:15];
  120. _numberLabel.textColor = UIColorFromRGB(0x333333);
  121. _numberLabel.text = @"数量";
  122. }
  123. return _numberLabel;
  124. }
  125. @end