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