口袋优选

KBScrollSearchView.m 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // KBScrollSearchView.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/1/16.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "KBScrollSearchView.h"
  9. const NSInteger titleFont = 14;
  10. @interface KBScrollSearchView()
  11. @property (nonatomic, strong) NSMutableArray *titles;
  12. @property (assign, nonatomic) int titleIndex;
  13. @property (assign, nonatomic) int index;
  14. @end
  15. @implementation KBScrollSearchView
  16. - (instancetype)initWithFrame:(CGRect)frame andTitles:(NSArray *)titles {
  17. self = [super initWithFrame:frame];
  18. if (self) {
  19. self.backgroundColor = [[UIColor YHColorWithHex:0x4B4B4B] colorWithAlphaComponent:0.20f];
  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.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
  28. btn.tag = self.index;
  29. [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
  30. [btn setTitle:self.titles[0] forState:UIControlStateNormal];
  31. NSLog(@"aaaaa");
  32. [btn setImage:[UIImage imageNamed:@"search_white"] forState:UIControlStateNormal];
  33. NSLog(@"aaaaa");
  34. btn.imageEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
  35. btn.titleEdgeInsets=UIEdgeInsetsMake(0, 12, 0, 0);
  36. btn.titleLabel.font = [UIFont systemFontOfSize:titleFont];
  37. [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  38. [self addSubview:btn];
  39. self.clipsToBounds = YES;
  40. [NSTimer scheduledTimerWithTimeInterval:4.0 target:self selector:@selector(nextButton) userInfo:nil repeats:YES];
  41. }
  42. return self;
  43. }
  44. -(void)nextButton{
  45. UIButton *firstBtn = [self viewWithTag:self.index];
  46. UIButton *modelBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, self.height, self.width, self.height)];
  47. modelBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
  48. modelBtn.tag = self.index + 1;
  49. if ([self.titles[self.titleIndex+1] isEqualToString:@""]) {
  50. self.titleIndex = -1;
  51. self.index = 0;
  52. }
  53. if (modelBtn.tag == self.titles.count) {
  54. modelBtn.tag = 1;
  55. }
  56. [modelBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
  57. [modelBtn setTitle:self.titles[self.titleIndex+1] forState:UIControlStateNormal];
  58. [modelBtn setImage:[UIImage imageNamed:@"search_white"] forState:UIControlStateNormal];
  59. modelBtn.titleLabel.font = [UIFont systemFontOfSize:titleFont];
  60. modelBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
  61. modelBtn.titleEdgeInsets=UIEdgeInsetsMake(0, 12, 0, 0);
  62. [modelBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  63. [self addSubview:modelBtn];
  64. [UIView animateWithDuration:0.25 animations:^{
  65. firstBtn.y = -self.height;
  66. modelBtn.y = 0;
  67. } completion:^(BOOL finished) {
  68. [firstBtn removeFromSuperview];
  69. } ];
  70. self.index++;
  71. self.titleIndex++;
  72. }
  73. -(void)clickBtn:(UIButton *)btn{
  74. if ([self.delegate respondsToSelector:@selector(clickTitleButton:)]) {
  75. [self.delegate clickTitleButton:btn];
  76. }
  77. }
  78. - (void)destory {
  79. for (UIView *subView in self.subviews) {
  80. [subView removeFromSuperview];
  81. }
  82. }
  83. @end