多选栏 LMSlider

来源:互联网 发布:广州易娱网络怎么样 编辑:程序博客网 时间:2024/06/09 21:49


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 = [[LMSlideralloc] 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 = [UIColorStringcolorWithHexString:@"343536"];

    }

    if (selectedTitleColor ==nil) {

        selectedTitleColor = [UIColorStringcolorWithHexString:@"ef9e49"];

    }

    self.userInteractionEnabled =YES;

    

    

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

        

    

        UIButton *btn = [UIButtonbuttonWithType:UIButtonTypeCustom];

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

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

        btn.titleLabel.font = [UIFontsystemFontOfSize:13];

        [btn setTitleColor:titleColorforState:UIControlStateNormal];

        [btn setTitleColor:selectedTitleColorforState:UIControlStateSelected];

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

        btn.tag =5202 + i;

        [selfaddSubview:btn];

        if (i==0) {

            [btn setSelected:YES];

        }

    

    }

    

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

    self.slider.backgroundColor = selectedTitleColor;

    [selfaddSubview:self.slider];

    

    returnself;

}


-(void)setSelectItem:(NSInteger)selectIndex {

    

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


    

}


// 按钮点击事件

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

    

//    [self handleSelectStuff:btn];

    _current = btn;

    [selfshutDownBtnSelected:btn.tag];

    NSInteger currentIndex = btn.tag -5202;

    [selfmoveSliderLabel:currentIndex];

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

        [self.delegatedidSelecItem:currentIndex];

    }

    

}


-(void)moveSliderLabel:(NSInteger)index {

    

    CGFloat WID =self.frame.size.width;

    CGFloat HEI =self.frame.size.height;

    CGFloat sliWID = WID/self.titleCount;

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

    


    

}


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

-(void)shutDownBtnSelected:(NSInteger)tag {

    

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

    for (UIView *btnin self.subviews) {

        if ([btnisKindOfClass:[UIButtonclass]]) {

            UIButton *cbtn = (UIButton *)btn;

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

                [cbtn setSelected:NO];

            }else {

                [sbtn setSelected:YES];

            }

        }

    }

}


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

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

    

    if (![_currentisEqual:btn]) {

        _count =0;

    }

    if ([_currentisEqual:btn]) {

        if (_count%2==0) {

            _count++;

            return;

        }

        _count++;

    }

    

}









@end