滑块跳转视图

滑块跳转视图##

效果:

滑块.gif

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

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


@interface BaseTopControl : UIControl <UIScrollViewDelegate>
{
    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下载

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,714评论 4 61
  • “换头手术”把多年来人类对精神、大脑和人体之间的关系的争议重新推上风口浪尖。如果换头成功,“我”还是我么? 11月...
    槽值阅读 11,020评论 18 50
  • 奶奶,你说小花还会回来么?一头银发的老人,看着泪眼朦胧的孙女,心疼的说道:“当然会,小花是幼猫时你就照顾它,它...
    花满秋阅读 3,153评论 6 8
  • 离开故乡已经二十多年,总以为对于80后的我们来说,没有前辈们难么浓厚的故土情结,甚至有时会避讳与人谈及自己...
    木樨简阅读 1,612评论 0 0

友情链接更多精彩内容