| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- //
- // SelecterToolsScrolView.m
- // SelecterTools
- //
- // Created by zhao on 16/3/15.
- // Copyright © 2016年 xincheng. All rights reserved.
- //
- #import "SelecterToolsScrolView.h"
- #define WIDTH [UIScreen mainScreen].bounds.size.width
- #define HEIGHT [UIScreen mainScreen].bounds.size.height
- #define TitleFont 15
- @interface SelecterToolsScrolView()
- @property(nonatomic,copy)BtnClick btnClick;
- @property(nonatomic,retain)NSMutableArray *btnArr;
- @property(nonatomic,retain)UIButton * previousBtn;
- @property(nonatomic,retain)UIButton * currentBtn;
- @property(nonatomic,retain)UIView *bottomScrLine;
- @end
- @implementation SelecterToolsScrolView
- /*
- // Only override drawRect: if you perform custom drawing.
- // An empty implementation adversely affects performance during animation.
- - (void)drawRect:(CGRect)rect {
- // Drawing code
- }
- */
- -(instancetype)initWithSeleterConditionTitleArr:(NSArray *)titleArr andBtnBlock:(BtnClick)btnClick
- {
- self = [super init];
- if (self) {
- self.frame = CGRectMake(-1,NavHeader,WIDTH-40, 40);
- self.backgroundColor = NewThemeColor;
- _btnArr = [NSMutableArray array];
- for (int i = 0; i<titleArr.count; i++) {
- UIButton *titleBtn = [[UIButton alloc]initWithFrame:CGRectMake(i*80,0, 80,40)];
- titleBtn.tag = i;
- [titleBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
- [titleBtn setTitle:titleArr[i] forState:UIControlStateNormal];
- titleBtn.titleLabel.font = [UIFont systemFontOfSize:TitleFont];
- [titleBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
- [titleBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
- [self addSubview:titleBtn];
- [_btnArr addObject:titleBtn];
- if (i == 0) {
- _previousBtn = titleBtn;
- _currentBtn = titleBtn;
- titleBtn.selected = YES;
- }
-
- }
- _bottomScrLine = [[UIView alloc]initWithFrame:CGRectMake(0, self.frame.size.height-4,80,2)];
- _bottomScrLine.center = CGPointMake(_currentBtn.center.x, _bottomScrLine.center.y);
- _bottomScrLine.backgroundColor = [UIColor whiteColor];
- [self addSubview:_bottomScrLine];
-
- self.contentSize = CGSizeMake(titleArr.count*80, 30);
- self.showsHorizontalScrollIndicator = NO;
- self.showsVerticalScrollIndicator = NO;
- // self.layer.borderWidth = 0.5;
- // self.layer.borderColor = [UIColor lightGrayColor].CGColor;
- self.btnClick = btnClick;
-
- }
- return self;
- }
- -(void)updateSelecterToolsIndex:(NSInteger )index
- {
- UIButton *selectBtn = _btnArr[index];
- [self changeSelectBtn:selectBtn];
- }
- -(void)btnClick:(UIButton *)sender
- {
- // [self changeSelectBtn:sender];
- self.btnClick(sender);
- }
- -(void)changeSelectBtn:(UIButton *)btn
- {
-
- _previousBtn = _currentBtn;
- _currentBtn = btn;
- _previousBtn.selected = NO;
- _currentBtn.selected = YES;
-
- [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
- _bottomScrLine.center = CGPointMake(_currentBtn.center.x, _bottomScrLine.center.y);
-
- } completion:^(BOOL finished) {
-
- }];
-
-
- if (_currentBtn.center.x<((WIDTH/2)-20)) {
-
- [self setContentOffset:CGPointMake(0, 0) animated:YES];
- }else if (_currentBtn.center.x>self.contentSize.width-((WIDTH/2)-20))
- {
- [self setContentOffset:CGPointMake(self.contentSize.width-(WIDTH-40), 0) animated:YES];
-
- }else
- {
- [self setContentOffset:CGPointMake(btn.center.x-((WIDTH/2)-20), 0) animated:YES];
- }
-
- }
- //
- //-(CGFloat)getTitleContentWidth:(NSString *)title
- //{
- // CGRect rect = [title boundingRectWithSize:CGSizeMake(WIDTH, 30) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:TitleFont]} context:nil];
- // return rect.size.width;
- //}
- @end
|