// // DRScrollSearchView.m // YouHuiProject // // Created by 小花 on 2018/1/16. // Copyright © 2018年 kuxuan. All rights reserved. // #import "DRScrollSearchView.h" const NSInteger titleFont = 14; @interface DRScrollSearchView(){ } @property (nonatomic, strong) NSMutableArray *titles; @property (assign, nonatomic) int titleIndex; @property (assign, nonatomic) int index; @property (assign, nonatomic) SearchScrollButtonStyle style; @end @implementation DRScrollSearchView - (instancetype)initWithFrame:(CGRect)frame andTitles:(NSArray *)titles { self = [super initWithFrame:frame]; if (self) { self.style = SearchScrollButtonGray; self.backgroundColor = [UIColor whiteColor]; self.layer.cornerRadius = self.height/2; self.titles = [NSMutableArray arrayWithArray:titles]; NSString *str = @""; [self.titles addObject:str]; self.index = 1; NSString *imgName; UIColor *color; switch (self.style) { case SearchScrollButtonWhite: { imgName = @"search_small"; color = [UIColor whiteColor]; } break; case SearchScrollButtonGray: { imgName = @"search_gray_small"; color = [UIColor YHColorWithHex:0x7F7F7F]; } break; default: { imgName = @"search_small"; color = [UIColor whiteColor]; } break; } UIButton *btn = [[UIButton alloc]init]; btn.frame = CGRectMake(0, 0, self.width, self.height); btn.tag = self.index; [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside]; [btn setTitle:self.titles[0] forState:UIControlStateNormal]; [btn setImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal]; [btn setTitleColor:color forState:UIControlStateNormal]; btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; btn.imageEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 00); btn.titleEdgeInsets = UIEdgeInsetsMake(0, 12, 0, 0); btn.titleLabel.font = [UIFont systemFontOfSize:titleFont]; [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.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; } NSString *imgName; UIColor *color; switch (self.style) { case SearchScrollButtonWhite: { imgName = @"search_small"; color = [UIColor whiteColor]; } break; case SearchScrollButtonGray: { imgName = @"search_gray_small"; color = [UIColor YHColorWithHex:0x7F7F7F]; } break; default: { imgName = @"search_small"; color = [UIColor whiteColor]; } break; } [modelBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside]; [modelBtn setTitle:self.titles[self.titleIndex+1] forState:UIControlStateNormal]; [modelBtn setImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal]; [modelBtn setTitleColor:color forState:UIControlStateNormal]; modelBtn.titleLabel.font = [UIFont systemFontOfSize:titleFont]; modelBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; modelBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0); modelBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 12, 0, 0); [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)setButtonStyle:(SearchScrollButtonStyle)style { self.style = style; NSString *imgName; UIColor *color; switch (self.style) { case SearchScrollButtonWhite: { imgName = @"search_small"; color = [UIColor whiteColor]; } break; case SearchScrollButtonGray: { imgName = @"search_gray_small"; color = [UIColor YHColorWithHex:0x7F7F7F]; } break; default: { imgName = @"search_small"; color = [UIColor whiteColor]; } break; } UIButton *firstBtn = [self viewWithTag:self.index]; [firstBtn setImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal]; [firstBtn setTitleColor:color forState:UIControlStateNormal]; } - (void)destory { for (UIView *subView in self.subviews) { [subView removeFromSuperview]; } } @end