酷店

KDPScreenViewController.m 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. //
  2. // KDPScreenViewController.m
  3. // KuDianProject
  4. //
  5. // Created by admin on 2019/7/8.
  6. // Copyright © 2019 KDP. All rights reserved.
  7. //
  8. #import "KDPScreenViewController.h"
  9. #import "KDPScreenBtnView.h"
  10. #define kBtnMargin 14
  11. #define kBaseBtnViewTag 2000
  12. @interface KDPScreenViewController ()<KDPScreenBtnViewDelegate>
  13. @property (nonatomic, strong) UILabel *tipLabel;
  14. @property (nonatomic, assign) BOOL shouldClear;
  15. @end
  16. @implementation KDPScreenViewController
  17. {
  18. BOOL _isPrice;
  19. }
  20. - (void)viewDidLoad {
  21. [super viewDidLoad];
  22. // Do any additional setup after loading the view.
  23. self.navBar.hidden = YES;
  24. self.shouldClear = NO;
  25. [self setContentView];
  26. }
  27. #pragma init
  28. - (instancetype)initWithPrice:(BOOL)price{
  29. if (self = [super init]) {
  30. _isPrice = price;
  31. }
  32. return self;
  33. }
  34. - (void)setContentView{
  35. self.tipLabel = [[UILabel alloc] initWithFrame:CGRectMake(13 + SCREEN_WIDTH/3, KDStatusHeight + 16, SCREEN_WIDTH-91-13, 20)];
  36. self.tipLabel.textColor = [UIColor colorWithHexString:@"0x333333"];
  37. self.tipLabel.font = [UIFont fontWithName:@"PingFang-SC-Medium" size: 14];
  38. NSString *tipText = _isPrice ? @"价格区间(元)":@"利润区间";
  39. self.tipLabel.text = tipText;
  40. [self.view addSubview:self.tipLabel];
  41. // 初始化tagView
  42. CGFloat btnWidth = (self.view.width - SCREEN_WIDTH/3 - kBtnMargin * 2 - 20)/2;
  43. CGFloat height = 45;
  44. NSArray *btnDataArray = _isPrice ? @[@"最低价",@"最高价",@"10元以下",@"10-50元",@"50-100元",@"100元以上"] : @[@"最低利润",@"",@"20%以上",@"30%以上",@"40%以上",@"50%以上"];
  45. for (NSInteger i = 0; i < 6; i++) {
  46. NSInteger row = i / 2;
  47. NSInteger line = i % 2;
  48. KDPScreenBtnView *btnView = [[KDPScreenBtnView alloc] initWithFrame:CGRectMake(kBtnMargin +SCREEN_WIDTH/3 + (btnWidth+20)*line,self.tipLabel.bottom + 25 + row *(17+height) , btnWidth, height)];
  49. btnView.delegate = self;
  50. btnView.tag = kBaseBtnViewTag + i;
  51. btnView.textColor = [UIColor colorWithHexString:@"#333333"];
  52. if (i < 2) {
  53. btnView.placeHolderText = btnDataArray[i];
  54. btnView.isEditing = YES;
  55. } else{
  56. btnView.text = btnDataArray[i];
  57. btnView.isEditing = NO;
  58. UITapGestureRecognizer *tapGest = [[UITapGestureRecognizer alloc] init];
  59. tapGest.numberOfTapsRequired = 1;
  60. [tapGest addTarget:self action:@selector(tapGestAction:)];
  61. [btnView addGestureRecognizer:tapGest];
  62. }
  63. [self.view addSubview:btnView];
  64. if (i == 1 && _isPrice == NO) {
  65. btnView.hidden = YES;
  66. }
  67. }
  68. // 重置按钮和确定按钮
  69. UIButton *resetBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  70. resetBtn.frame = CGRectMake(SCREEN_WIDTH/3 + 14, self.tipLabel.bottom + 235, (SCREEN_WIDTH-SCREEN_WIDTH/3-28)/2, 41);
  71. // [resetBtn setGradientBackgroundWithColors:@[[UIColor colorWithRGB:0xFFC300],[UIColor colorWithRGB:0xFF9A01]] locations:@[@0,@1] startPoint:CGPointMake(0, 0) endPoint:CGPointMake(0, 1)];
  72. resetBtn.backgroundColor = [UIColor colorWithHex:0xFF7693];
  73. [resetBtn addCornerRadiusByRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomLeft cornerRadii:CGSizeMake(20, 20)];
  74. resetBtn.showsTouchWhenHighlighted = NO;
  75. [resetBtn setTitle:@"重置" forState:UIControlStateNormal];
  76. [resetBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  77. resetBtn.titleLabel.font = FONT_SYS(14);
  78. [resetBtn addTarget:self action:@selector(resetAction:) forControlEvents:UIControlEventTouchUpInside];
  79. [self.view addSubview:resetBtn];
  80. UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  81. confirmBtn.frame = CGRectMake(resetBtn.right, self.tipLabel.bottom + 235, (SCREEN_WIDTH-SCREEN_WIDTH/3-28)/2, 41);
  82. // [confirmBtn setGradientBackgroundWithColors:@[[UIColor colorWithRGB:0xFF8300],[UIColor colorWithRGB:0xFF5200]] locations:@[@0,@1] startPoint:CGPointMake(0, 0) endPoint:CGPointMake(0, 1)];
  83. confirmBtn.backgroundColor = [UIColor colorWithHex:0xFF6B8A];
  84. [confirmBtn addCornerRadiusByRoundingCorners:UIRectCornerTopRight | UIRectCornerBottomRight cornerRadii:CGSizeMake(20, 20)];
  85. confirmBtn.showsTouchWhenHighlighted = NO;
  86. [confirmBtn setTitle:@"确定" forState:UIControlStateNormal];
  87. [confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  88. confirmBtn.titleLabel.font = FONT_SYS(14);
  89. [confirmBtn addTarget:self action:@selector(confirmAction:) forControlEvents:UIControlEventTouchUpInside];
  90. [self.view addSubview:confirmBtn];
  91. }
  92. #pragma mark event handle
  93. - (void)tapGestAction:(UITapGestureRecognizer *)tap{
  94. [self resetBtnView];
  95. KDPScreenBtnView *btnView = (KDPScreenBtnView *)tap.view;
  96. btnView.textColor = [UIColor baseColor];
  97. self.shouldClear = NO;
  98. [self setUpDataWithSelectTag:btnView.tag];
  99. }
  100. - (void)resetBtnView{
  101. [self.view.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  102. if ([obj isKindOfClass:[KDPScreenBtnView class]]) {
  103. KDPScreenBtnView *btnView = (KDPScreenBtnView *)obj;
  104. if (btnView.tag > kBaseBtnViewTag + 1) {
  105. btnView.textColor = [UIColor colorWithHex:0x333333];
  106. }
  107. }
  108. }];
  109. }
  110. - (void)setUpDataWithSelectTag:(NSInteger )btnTag{
  111. if (_isPrice == YES) {
  112. [self setUpPriceTextWithTag:btnTag];
  113. } else{
  114. [self setUpProfitTextWithTag:btnTag];
  115. }
  116. }
  117. - (void)setUpPriceTextWithTag:(NSInteger)btnTag{
  118. NSString *mineString = @"";
  119. NSString *maxString = @"";
  120. if (btnTag == kBaseBtnViewTag + 2) {
  121. mineString = @"0";
  122. maxString = @"10";
  123. } else if (btnTag == kBaseBtnViewTag + 3){
  124. mineString = @"10";
  125. maxString = @"50";
  126. } else if (btnTag == kBaseBtnViewTag + 4){
  127. mineString = @"50";
  128. maxString = @"100";
  129. } else if (btnTag == kBaseBtnViewTag + 5){
  130. mineString = @"100";
  131. }
  132. KDPScreenBtnView *mineView = [self.view viewWithTag:kBaseBtnViewTag];
  133. KDPScreenBtnView *maxView = [self.view viewWithTag:kBaseBtnViewTag+1];
  134. mineView.text = mineString;
  135. maxView.text = maxString;
  136. }
  137. - (void)setUpProfitTextWithTag:(NSInteger )btnTag{
  138. NSString *profitString = @"";
  139. if (btnTag == kBaseBtnViewTag + 2) {
  140. profitString = @"20";
  141. } else if (btnTag == kBaseBtnViewTag + 3){
  142. profitString = @"30";
  143. } else if (btnTag == kBaseBtnViewTag + 4){
  144. profitString = @"40";
  145. } else if (btnTag == kBaseBtnViewTag + 5){
  146. profitString = @"50";
  147. }
  148. KDPScreenBtnView *proFileView = [self.view viewWithTag:kBaseBtnViewTag];
  149. proFileView.text = profitString;
  150. }
  151. - (void)resetAction:(UIButton *)sender{
  152. [self resetBtnView];
  153. KDPScreenBtnView *mineView = [self.view viewWithTag:kBaseBtnViewTag];
  154. KDPScreenBtnView *maxView = [self.view viewWithTag:kBaseBtnViewTag+1];
  155. mineView.text = @"";
  156. maxView.text = @"";
  157. [self dismissViewControllerAnimated:YES completion:nil];
  158. if (self.resetBlock) {
  159. self.resetBlock(_isPrice);
  160. }
  161. }
  162. - (void)confirmAction:(UIButton *)sender{
  163. KDPScreenBtnView *mineView = [self.view viewWithTag:kBaseBtnViewTag];
  164. KDPScreenBtnView *maxView = [self.view viewWithTag:kBaseBtnViewTag+1];
  165. [self dismissViewControllerAnimated:YES completion:nil];
  166. if (_isPrice) {
  167. self.priceBlock(mineView.text, maxView.text);
  168. } else{
  169. self.profitBlock(mineView.text);
  170. }
  171. }
  172. - (void)textFieldValueChanged:(UITextField *)textfield{
  173. KDPScreenBtnView *btnView = (KDPScreenBtnView *)textfield.superview;
  174. KDPScreenBtnView *minView = [self.view viewWithTag:kBaseBtnViewTag];
  175. KDPScreenBtnView *maxView = [self.view viewWithTag:kBaseBtnViewTag + 1];
  176. if (_isPrice == NO) {
  177. [self setBtnColorWithProfitText:minView.text];
  178. if (btnView.tag == kBaseBtnViewTag) {
  179. if ([textfield.text integerValue] >= 100) {
  180. [MBProgressHUD showError:@"利润不可超过100%"];
  181. textfield.text = [textfield.text substringToIndex:textfield.text.length-1];
  182. }
  183. }
  184. } else{
  185. [self setBtnColorWithMinText:minView.text maxText:maxView.text];
  186. }
  187. }
  188. - (void)setParams:(NSDictionary *)params{
  189. _params = params;
  190. NSString *minText = [params valueForKey:@"start_price"];
  191. NSString *maxText = [params valueForKey:@"end_price"];
  192. NSString *profitText = [params valueForKey:@"start_tk_rate"];
  193. KDPScreenBtnView *mineBtnView = [self.view viewWithTag:kBaseBtnViewTag];
  194. KDPScreenBtnView *maxBtnView = [self.view viewWithTag:kBaseBtnViewTag+1];
  195. if (_isPrice == YES) {
  196. mineBtnView.text = minText;
  197. maxBtnView.text = maxText;
  198. [self setBtnColorWithMinText:minText maxText:maxText];
  199. } else{
  200. mineBtnView.text = profitText;
  201. [self setBtnColorWithProfitText:profitText];
  202. }
  203. }
  204. - (void)setBtnColorWithMinText:(NSString *)minText maxText:(NSString *)maxText{
  205. KDPScreenBtnView *tenBtnView = [self.view viewWithTag:kBaseBtnViewTag + 2];
  206. KDPScreenBtnView *fiveBtnView = [self.view viewWithTag:kBaseBtnViewTag + 3];
  207. KDPScreenBtnView *thirdBtnView = [self.view viewWithTag:kBaseBtnViewTag + 4];
  208. KDPScreenBtnView *fourBtnView = [self.view viewWithTag:kBaseBtnViewTag + 5];
  209. self.shouldClear = NO;
  210. if ([minText integerValue] == 0 && [maxText integerValue] == 10) {
  211. tenBtnView.textColor = [UIColor baseColor];
  212. } else if ([minText integerValue] == 10 && [maxText integerValue] == 50){
  213. fiveBtnView.textColor = [UIColor baseColor];
  214. } else if ([minText integerValue] == 50 && [maxText integerValue] == 100){
  215. thirdBtnView.textColor = [UIColor baseColor];
  216. } else if ([minText integerValue] == 100 && [maxText integerValue] == 0){
  217. fourBtnView.textColor = [UIColor baseColor];
  218. } else{
  219. self.shouldClear = YES;
  220. [self resetBtnView];
  221. }
  222. }
  223. - (void)setBtnColorWithProfitText:(NSString *)profit{
  224. KDPScreenBtnView *firstBtnView = [self.view viewWithTag:kBaseBtnViewTag + 2];
  225. KDPScreenBtnView *twoBtnView = [self.view viewWithTag:kBaseBtnViewTag + 3];
  226. KDPScreenBtnView *thirdBtnView = [self.view viewWithTag:kBaseBtnViewTag + 4];
  227. KDPScreenBtnView *fourBtnView = [self.view viewWithTag:kBaseBtnViewTag + 5];
  228. self.shouldClear = NO;
  229. if ([profit integerValue] == 20) {
  230. firstBtnView.textColor = [UIColor baseColor];
  231. } else if ([profit integerValue] == 30){
  232. twoBtnView.textColor = [UIColor baseColor];
  233. } else if ([profit integerValue] == 40){
  234. thirdBtnView.textColor = [UIColor baseColor];
  235. } else if ([profit integerValue] == 50){
  236. fourBtnView.textColor = [UIColor baseColor];
  237. } else{
  238. self.shouldClear = YES;
  239. [self resetBtnView];
  240. }
  241. }
  242. - (void)textFieldDidEndEditing:(UITextField *)textField{
  243. KDPScreenBtnView *mineBtnView = [self.view viewWithTag:kBaseBtnViewTag];
  244. KDPScreenBtnView *maxBtnView = [self.view viewWithTag:kBaseBtnViewTag+1];
  245. if (_isPrice == YES) { // 价格
  246. if ([mineBtnView.text integerValue] > 0 && [maxBtnView.text integerValue] > 0 && [maxBtnView.text integerValue] < [mineBtnView.text integerValue]) {
  247. [MBProgressHUD showError:@"请输入正确的价格"];
  248. textField.text = @"";
  249. }
  250. }
  251. }
  252. - (void)textFieldDidBeginEditing:(UITextField *)textfield{
  253. if (self.shouldClear == YES) {
  254. [self resetBtnView];
  255. }
  256. }
  257. @end