《省钱达人》与《猎豆优选》UI相同版。域名tbk

DRScrollSearchView.m 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. //
  2. // DRScrollSearchView.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/1/16.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "DRScrollSearchView.h"
  9. const NSInteger titleFont = 14;
  10. @interface DRScrollSearchView(){
  11. }
  12. @property (nonatomic, strong) NSMutableArray *titles;
  13. @property (assign, nonatomic) int titleIndex;
  14. @property (assign, nonatomic) int index;
  15. @property (assign, nonatomic) SearchScrollButtonStyle style;
  16. @end
  17. @implementation DRScrollSearchView
  18. - (instancetype)initWithFrame:(CGRect)frame andTitles:(NSArray *)titles {
  19. self = [super initWithFrame:frame];
  20. if (self) {
  21. self.style = SearchScrollButtonGray;
  22. self.backgroundColor = [UIColor whiteColor];
  23. self.layer.cornerRadius = self.height/2;
  24. self.titles = [NSMutableArray arrayWithArray:titles];
  25. NSString *str = @"";
  26. [self.titles addObject:str];
  27. self.index = 1;
  28. NSString *imgName;
  29. UIColor *color;
  30. switch (self.style) {
  31. case SearchScrollButtonWhite:
  32. {
  33. imgName = @"search_small";
  34. color = [UIColor whiteColor];
  35. }
  36. break;
  37. case SearchScrollButtonGray:
  38. {
  39. imgName = @"search_gray_small";
  40. color = [UIColor YHColorWithHex:0x7F7F7F];
  41. }
  42. break;
  43. default:
  44. {
  45. imgName = @"search_small";
  46. color = [UIColor whiteColor];
  47. }
  48. break;
  49. }
  50. UIButton *btn = [[UIButton alloc]init];
  51. btn.frame = CGRectMake(0, 0, self.width, self.height);
  52. btn.tag = self.index;
  53. [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
  54. [btn setTitle:self.titles[0] forState:UIControlStateNormal];
  55. [btn setImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
  56. [btn setTitleColor:color forState:UIControlStateNormal];
  57. btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
  58. btn.imageEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 00);
  59. btn.titleEdgeInsets = UIEdgeInsetsMake(0, 12, 0, 0);
  60. btn.titleLabel.font = [UIFont systemFontOfSize:titleFont];
  61. [self addSubview:btn];
  62. self.clipsToBounds = YES;
  63. [NSTimer scheduledTimerWithTimeInterval:4.0 target:self selector:@selector(nextButton) userInfo:nil repeats:YES];
  64. }
  65. return self;
  66. }
  67. -(void)nextButton{
  68. UIButton *firstBtn = [self viewWithTag:self.index];
  69. UIButton *modelBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, self.height, self.width, self.height)];
  70. modelBtn.tag = self.index + 1;
  71. if ([self.titles[self.titleIndex+1] isEqualToString:@""]) {
  72. self.titleIndex = -1;
  73. self.index = 0;
  74. }
  75. if (modelBtn.tag == self.titles.count) {
  76. modelBtn.tag = 1;
  77. }
  78. NSString *imgName;
  79. UIColor *color;
  80. switch (self.style) {
  81. case SearchScrollButtonWhite:
  82. {
  83. imgName = @"search_small";
  84. color = [UIColor whiteColor];
  85. }
  86. break;
  87. case SearchScrollButtonGray:
  88. {
  89. imgName = @"search_gray_small";
  90. color = [UIColor YHColorWithHex:0x7F7F7F];
  91. }
  92. break;
  93. default:
  94. {
  95. imgName = @"search_small";
  96. color = [UIColor whiteColor];
  97. }
  98. break;
  99. }
  100. [modelBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
  101. [modelBtn setTitle:self.titles[self.titleIndex+1] forState:UIControlStateNormal];
  102. [modelBtn setImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
  103. [modelBtn setTitleColor:color forState:UIControlStateNormal];
  104. modelBtn.titleLabel.font = [UIFont systemFontOfSize:titleFont];
  105. modelBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
  106. modelBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
  107. modelBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 12, 0, 0);
  108. [self addSubview:modelBtn];
  109. [UIView animateWithDuration:0.25 animations:^{
  110. firstBtn.y = -self.height;
  111. modelBtn.y = 0;
  112. } completion:^(BOOL finished) {
  113. [firstBtn removeFromSuperview];
  114. } ];
  115. self.index++;
  116. self.titleIndex++;
  117. }
  118. -(void)clickBtn:(UIButton *)btn{
  119. if ([self.delegate respondsToSelector:@selector(clickTitleButton:)]) {
  120. [self.delegate clickTitleButton:btn];
  121. }
  122. }
  123. - (void)setButtonStyle:(SearchScrollButtonStyle)style {
  124. self.style = style;
  125. NSString *imgName;
  126. UIColor *color;
  127. switch (self.style) {
  128. case SearchScrollButtonWhite:
  129. {
  130. imgName = @"search_small";
  131. color = [UIColor whiteColor];
  132. }
  133. break;
  134. case SearchScrollButtonGray:
  135. {
  136. imgName = @"search_gray_small";
  137. color = [UIColor YHColorWithHex:0x7F7F7F];
  138. }
  139. break;
  140. default:
  141. {
  142. imgName = @"search_small";
  143. color = [UIColor whiteColor];
  144. }
  145. break;
  146. }
  147. UIButton *firstBtn = [self viewWithTag:self.index];
  148. [firstBtn setImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
  149. [firstBtn setTitleColor:color forState:UIControlStateNormal];
  150. }
  151. - (void)destory {
  152. for (UIView *subView in self.subviews) {
  153. [subView removeFromSuperview];
  154. }
  155. }
  156. @end