// // KBScrollSearchView.m // YouHuiProject // // Created by 小花 on 2018/1/16. // Copyright © 2018年 kuxuan. All rights reserved. // #import "KBScrollSearchView.h" const NSInteger titleFont = 14; @interface KBScrollSearchView() @property (nonatomic, strong) NSMutableArray *titles; @property (assign, nonatomic) int titleIndex; @property (assign, nonatomic) int index; @end @implementation KBScrollSearchView - (instancetype)initWithFrame:(CGRect)frame andTitles:(NSArray *)titles { self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [[UIColor YHColorWithHex:0x4B4B4B] colorWithAlphaComponent:0.20f]; self.layer.cornerRadius = self.height/2; self.titles = [NSMutableArray arrayWithArray:titles]; NSString *str = @""; [self.titles addObject:str]; self.index = 1; UIButton *btn = [[UIButton alloc]init]; btn.frame = CGRectMake(0, 0, self.width, self.height); btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; btn.tag = self.index; [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside]; [btn setTitle:self.titles[0] forState:UIControlStateNormal]; NSLog(@"aaaaa"); [btn setImage:[UIImage imageNamed:@"search_white"] forState:UIControlStateNormal]; NSLog(@"aaaaa"); btn.imageEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0); btn.titleEdgeInsets=UIEdgeInsetsMake(0, 12, 0, 0); btn.titleLabel.font = [UIFont systemFontOfSize:titleFont]; [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [self addSubview:btn]; self.clipsToBounds = YES; [NSTimer scheduledTimerWithTimeInterval:4.0 target:self selector:@selector(nextButton) userInfo:nil repeats:YES]; } return self; } -(void)nextButton{ UIButton *firstBtn = [self viewWithTag:self.index]; UIButton *modelBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, self.height, self.width, self.height)]; modelBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; modelBtn.tag = self.index + 1; if ([self.titles[self.titleIndex+1] isEqualToString:@""]) { self.titleIndex = -1; self.index = 0; } if (modelBtn.tag == self.titles.count) { modelBtn.tag = 1; } [modelBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside]; [modelBtn setTitle:self.titles[self.titleIndex+1] forState:UIControlStateNormal]; [modelBtn setImage:[UIImage imageNamed:@"search_white"] forState:UIControlStateNormal]; modelBtn.titleLabel.font = [UIFont systemFontOfSize:titleFont]; modelBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0); modelBtn.titleEdgeInsets=UIEdgeInsetsMake(0, 12, 0, 0); [modelBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [self addSubview:modelBtn]; [UIView animateWithDuration:0.25 animations:^{ firstBtn.y = -self.height; modelBtn.y = 0; } completion:^(BOOL finished) { [firstBtn removeFromSuperview]; } ]; self.index++; self.titleIndex++; } -(void)clickBtn:(UIButton *)btn{ if ([self.delegate respondsToSelector:@selector(clickTitleButton:)]) { [self.delegate clickTitleButton:btn]; } } - (void)destory { for (UIView *subView in self.subviews) { [subView removeFromSuperview]; } } @end