HeadChooseView头部视图滑动

#import <UIKit/UIKit.h>

#define Button_Origin_Tag 78

@interface HeadChooseView : UIView

//外部决定有多少模块
@property (nonatomic,strong) NSArray * items;

//内部选中某一个模块,传递给外部
@property (nonatomic,copy) void(^itemClickAtIndex)(NSInteger index);

//由外部决定选中哪一个模块
-(void)setSelectAtIndex:(NSInteger)index;
-(void)buttonClick:(UIButton*)button;



@end

#import "HeadChooseView.h"

@implementation HeadChooseView

{
    UIView * _redLine ;
    CGFloat _wid;
}
-(instancetype)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        
        _wid = frame.size.width;
        
    }
    return self;
}
-(void)setItems:(NSArray *)items
{
    _items = items;
    
    //先清空当前视图上的所有子视图
    for (UIView * view in self.subviews) {
        [view removeFromSuperview];
    }
    
    //添加按钮
    CGFloat itemWidth = _wid/items.count;
    CGFloat itemHeight = self.frame.size.height-2;
    
    for (int i = 0; i< items.count;i++ ) {
        UIButton * buton = [[UIButton alloc]init];
        buton.frame = CGRectMake(i*itemWidth, 0, itemWidth, itemHeight);
        [buton setTitle:items[i] forState:UIControlStateNormal];
        buton.titleLabel.font = LabelFount(14);
        
        [buton setTitleColor:[UIColor grayColor]forState:UIControlStateNormal];
        [buton setTitleColor:kMainColor forState:UIControlStateSelected];
        [buton addTarget:self action:@selector(bntClicked:) forControlEvents:UIControlEventTouchDown];
        buton.tag = Button_Origin_Tag+i;
        [self addSubview:buton];
        
        if (i == 0) {
            buton.selected = YES;
        }
        
    }
    
    //加红线
    _redLine = [[UIView alloc]init];
    _redLine.frame = CGRectMake(0, self.frame.size.height-2, itemWidth, 2);
    _redLine.backgroundColor = kMainColor;
    [self addSubview:_redLine];
    
}

-(void)buttonClick:(UIButton*)button
{
    button.enabled = NO;
    //获取点击的是第几个button
    NSInteger index = button.tag - Button_Origin_Tag;
    [self setSelectAtIndex:index];
    
    //2、把事件传递出去
    if (self.itemClickAtIndex) {
        _itemClickAtIndex(index);
        button.enabled = YES;
    }
}

- (void)bntClicked:(id)sender{
    
    [[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(buttonClick:) object:sender];
    [self performSelector:@selector(buttonClick:)withObject:sender afterDelay:0.2];
    
}

-(void)setSelectAtIndex:(NSInteger)index
{
    //1、先调整自身的视图显示
    for (int i = 0; i < self.items.count; i++) {
        UIButton * bt = [self viewWithTag:i+Button_Origin_Tag];
        
        if (bt.tag-Button_Origin_Tag == index) {
            bt.selected = YES;
            
        }else{
            bt.selected = NO;
        }
    }
    //调整红线的位置
    [UIView animateWithDuration:0.4 animations:^{
        CGRect rect = _redLine.frame;
        rect.origin.x = index*rect.size.width;
        _redLine.frame = rect;
    }];
}
@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值