省钱达人

DRScrollSearchView.m 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. @property (nonatomic, strong) NSMutableArray *titles;
  12. @property (assign, nonatomic) int titleIndex;
  13. @property (assign, nonatomic) int index;
  14. @end
  15. @implementation DRScrollSearchView
  16. - (instancetype)initWithFrame:(CGRect)frame andTitles:(NSArray *)titles {
  17. self = [super initWithFrame:frame];
  18. if (self) {
  19. self.backgroundColor = [UIColor YHColorWithHex:0xF5F5F5];
  20. self.layer.cornerRadius = self.height/2;
  21. self.titles = [NSMutableArray arrayWithArray:titles];
  22. NSString *str = @"";
  23. [self.titles addObject:str];
  24. self.index = 1;
  25. UIButton *btn = [[UIButton alloc]init];
  26. btn.frame = CGRectMake(0, 0, self.width, self.height);
  27. btn.tag = self.index;
  28. [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
  29. [btn setTitle:self.titles[0] forState:UIControlStateNormal];
  30. [btn setImage:[UIImage imageNamed:@"search_small"] forState:UIControlStateNormal];
  31. btn.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
  32. btn.titleLabel.font = [UIFont systemFontOfSize:titleFont];
  33. [btn setTitleColor:[UIColor YHColorWithHex:0x999999] forState:UIControlStateNormal];
  34. [self addSubview:btn];
  35. self.clipsToBounds = YES;
  36. [NSTimer scheduledTimerWithTimeInterval:4.0 target:self selector:@selector(nextButton) userInfo:nil repeats:YES];
  37. }
  38. return self;
  39. }
  40. -(void)nextButton{
  41. UIButton *firstBtn = [self viewWithTag:self.index];
  42. UIButton *modelBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, self.height, self.width, self.height)];
  43. modelBtn.tag = self.index + 1;
  44. if ([self.titles[self.titleIndex+1] isEqualToString:@""]) {
  45. self.titleIndex = -1;
  46. self.index = 0;
  47. }
  48. if (modelBtn.tag == self.titles.count) {
  49. modelBtn.tag = 1;
  50. }
  51. [modelBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
  52. [modelBtn setTitle:self.titles[self.titleIndex+1] forState:UIControlStateNormal];
  53. [modelBtn setImage:[UIImage imageNamed:@"search_small"] forState:UIControlStateNormal];
  54. modelBtn.titleLabel.font = [UIFont systemFontOfSize:titleFont];
  55. modelBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
  56. [modelBtn setTitleColor:[UIColor YHColorWithHex:0x999999] forState:UIControlStateNormal];
  57. [self addSubview:modelBtn];
  58. [UIView animateWithDuration:0.25 animations:^{
  59. firstBtn.y = -self.height;
  60. modelBtn.y = 0;
  61. } completion:^(BOOL finished) {
  62. [firstBtn removeFromSuperview];
  63. } ];
  64. self.index++;
  65. self.titleIndex++;
  66. }
  67. -(void)clickBtn:(UIButton *)btn{
  68. if ([self.delegate respondsToSelector:@selector(clickTitleButton:)]) {
  69. [self.delegate clickTitleButton:btn];
  70. }
  71. }
  72. - (void)destory {
  73. for (UIView *subView in self.subviews) {
  74. [subView removeFromSuperview];
  75. }
  76. }
  77. @end