滑块跳转视图

滑块跳转视图##

效果:

滑块.gif

封装了继承UIcontrol的类BaseTopControl
BaseTopControl.h实现如下:

#import "Masonry.h"
#import "UIView+Masonry_LJC.h"


@interface BaseTopControl : UIControl 
{
    UIImageView *_squreView;
}

@property (copy, nonatomic) void (^SelectBlock)(NSInteger);
@property (assign, nonatomic) NSInteger selectIndex;
@property (assign, nonatomic) NSInteger count;

- (instancetype)initWithTitles:(NSArray *)titles;

BaseTopControl.m实现如下:

- (instancetype)initWithTitles:(NSArray *)titles
{
    if (self = [super init]) {
        
        self.backgroundColor = [UIColor whiteColor];
        
        _squreView = [UIImageView new];
        _squreView.backgroundColor =[UIColor blueColor];
        [self addSubview:_squreView];
        
        self.count = titles.count;
        
        NSMutableArray *btns = [NSMutableArray array];
        for (int i = 0; i < titles.count; i++) {
            
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.titleLabel.font = [UIFont boldSystemFontOfSize:14];
            [btn setTitle:titles[i] forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
            btn.tag = i+1;
            [btn addTarget:self action:@selector(selectAction:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:btn];
            [btns addObject:btn];
        }
        
        [self distributeSpacingHorizontallyWith:btns];
    }
    
    return self;
}

#pragma mark - action
- (void)selectAction:(UIButton *)btn
{
    for (int i = 1; i < self.count+1; i++) {
        
        UIButton *botton = (UIButton *)[self viewWithTag:i];
        botton.selected = NO;
    }
    
    btn.selected = YES;
    
    CGFloat width = [btn.titleLabel.text sizeWithFont:btn.titleLabel.font constrainedToSize:CGSizeMake(1000, 1000)].width;
    [_squreView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(self);
        make.height.mas_equalTo(@2);
        make.centerX.equalTo(btn);
        make.width.mas_equalTo(width + 10);
    }];
    
    [UIView animateWithDuration:0.2 animations:^{
       
        [self layoutIfNeeded];
    }];
    
    _selectIndex = btn.tag - 1;
    
    [self sendActionsForControlEvents:UIControlEventValueChanged];
}

- (void)setSelectIndex:(NSInteger)selectIndex
{
    _selectIndex = selectIndex;
    
    UIButton *btn = [self viewWithTag:selectIndex + 1];
    [self selectAction:btn];
}

在控制器中进行初始化:


    _seg = [[BaseTopControl alloc] initWithTitles:@[@"历史", @"现在",@"未来"]];
    [_seg addTarget:self action:@selector(clickSegmentAction) forControlEvents:UIControlEventValueChanged];
    _seg.selectIndex = 0;
    [self.view addSubview:_seg];
    
    _contentSC = [UIScrollView new];
    _contentSC.scrollEnabled = NO;
    _contentSC.delegate = self;
    
    [self.view addSubview:_contentSC];

// 点击滑块的响应方法

- (void)clickSegmentAction
{
    [_contentSC setContentOffset:CGPointMake(_contentSC.frame.size.width * _seg.selectIndex, 0) animated:YES];
}

然后把需要滑动的tableView或View添加到scrollView上即可:详情下载Demo

Demo下载

你可能感兴趣的:(滑块跳转视图)