多选栏 LMSlider


demo连接:https://github.com/SunshineTraveller/LMSlider


开发中经常会遇到下面这种多选栏



下面是我封装的一个选择栏


.h  文件 


#import <UIKit/UIKit.h>


@protocol LMSliderProtocol <NSObject>



/**

 代理方法

 @param item 选中item回调

 */

-(void)didSelecItem:(NSInteger)item;



@end



@interface LMSlider : UIView



/** 代理 */

@property (nonatomic,weak) id<LMSliderProtocol> delegate;



/**

 构造方法

 @param frame frame

 @param titles title

 @param titleColor title颜色  不传默认为黑色

 @param selectedTitleColor title选中颜色  不传默认为橙色

 @return self

 */

-(instancetype)initWithFrame:(CGRect)frame titles:(NSArray *)titles titleColor:(UIColor *)titleColor selectedTitleColor:(UIColor *)selectedTitleColor;




/**

 设置当前选中item

 默认为第0个选中

 @param selectIndex 选中的位数

 */

-(void)setSelectItem:(NSInteger)selectIndex;



@end




.m文件

#import "UIColorString.h"

#import "LMSlider.h"


@interface LMSlider() {

    

    NSInteger _count;

    UIButton *_current;  // 记录当前点击按钮

    

}


@property (nonatomic,strong) UILabel *slider;


/** title颜色 */

@property (nonatomic,strong) UIColor *titleColor;


/** 选中颜色 */

@property (nonatomic,strong) UIColor *selectedTitleColor;


/** title个数 */

@property (nonatomic,assign) NSInteger titleCount;



@end


@implementation LMSlider


-(instancetype)initWithFrame:(CGRect)frame titles:(NSArray *)titles titleColor:(UIColor *)titleColor selectedTitleColor:(UIColor *)selectedTitleColor {

    

    self = [[LMSlider alloc] init];

    self.frame = frame;

    CGFloat WID = self.frame.size.width;

    CGFloat HEI = self.frame.size.height;

    NSInteger count = titles.count;

    self.titleCount = count;

    if (titleColor == nil){

        titleColor = [UIColorString colorWithHexString:@"343536"];

    }

    if (selectedTitleColor == nil) {

        selectedTitleColor = [UIColorString colorWithHexString:@"ef9e49"];

    }

    self.userInteractionEnabled = YES;

    

    

    for (int i = 0; i<count; i++) {

        

    

        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

        btn.frame = CGRectMake(i*WID/count, 0, WID/count, HEI-1.5);

        [btn setTitle:titles[i] forState:UIControlStateNormal];

        btn.titleLabel.font = [UIFont systemFontOfSize:13];

        [btn setTitleColor:titleColor forState:UIControlStateNormal];

        [btn setTitleColor:selectedTitleColor forState:UIControlStateSelected];

        [btn addTarget:self action:@selector(didSelecBtn:) forControlEvents:UIControlEventTouchUpInside];

        btn.tag = 5202 + i;

        [self addSubview:btn];

        if (i==0) {

            [btn setSelected:YES];

        }

    

    }

    

    self.slider = [[UILabel alloc] initWithFrame:CGRectMake(0, HEI-1.5, WID/count, 1.5)];

    self.slider.backgroundColor = selectedTitleColor;

    [self addSubview:self.slider];

    

    return self;

}


-(void)setSelectItem:(NSInteger)selectIndex {

    

    if (selectIndex >= self.titleCount) NSLog(@"    **********\n    当前选中item不能超过title的总数,请检查后再调用!\n    ********");return;


    

}


// 按钮点击事件

-(void)didSelecBtn:(UIButton *)btn {

    

//    [self handleSelectStuff:btn];

    _current = btn;

    [self shutDownBtnSelected:btn.tag];

    NSInteger currentIndex = btn.tag - 5202;

    [self moveSliderLabel:currentIndex];

    if ([self.delegate respondsToSelector:@selector(didSelecItem:)]) {

        [self.delegate didSelecItem:currentIndex];

    }

    

}


-(void)moveSliderLabel:(NSInteger)index {

    

    CGFloat WID = self.frame.size.width;

    CGFloat HEI = self.frame.size.height;

    CGFloat sliWID = WID/self.titleCount;

    [self.slider setFrame:CGRectMake(index*sliWID, HEI-1.5, sliWID, 1.5)];

    


    

}


/** 关闭btn选中状态  */

-(void)shutDownBtnSelected:(NSInteger)tag {

    

    UIButton *sbtn = (UIButton *)[self viewWithTag:tag];  // 取出当前btn

    for (UIView *btn in self.subviews) {

        if ([btn isKindOfClass:[UIButton class]]) {

            UIButton *cbtn = (UIButton *)btn;

            if (![cbtn.titleLabel.text isEqualToString:sbtn.titleLabel.text]) {

                [cbtn setSelected:NO];

            }else {

                [sbtn setSelected:YES];

            }

        }

    }

}


/** 处理重复点击事件 */

-(void)handleSelectStuff:(UIButton *)btn {

    

    if (![_current isEqual:btn]) {

        _count = 0;

    }

    if ([_current isEqual:btn]) {

        if (_count%2==0) {

            _count++;

            return;

        }

        _count++;

    }

    

}









@end






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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值