天天看點

滑動工具條

滑動工具條一般有兩種:

一:左右滑動,有左右邊界,無法循環滑動的工具條。

二:左右循環滑動的工具條。

咱們使用的大部分是第一類非循環的工具條。

BGCommonScrollToolPanel.h

#import

@interface BGCommonScrollToolPanel : UIView

@property (nonatomic, copy) void(^hitCallback)(NSInteger index);
@property (nonatomic, assign) BOOL isSendindRequest;

- (instancetype)initWithFrame:(CGRect)frame underlineColor:(UIColor *)underlineColor TitleArr:(NSMutableArray *)titleArr textColor:(UIColor *)textColor selectTextColor:(UIColor *)selectTextColor;      
#import

#define
#define
#define
#define

@interface BGCommonScrollToolPanel ()<UIScrollViewDelegate>
@property (nonatomic,strong) UIScrollView *alertBackgroundView;
@property (nonatomic,strong) NSMutableArray *titleBtnsArr;
@property (nonatomic,strong) UIPanGestureRecognizer *painRecognize1;
@property (nonatomic,strong) NSMutableArray *titleArr;
@property (nonatomic,strong) NSMutableArray *titleBtnWidthArr;
//@property (nonatomic,strong) UIView *lineView;
@property (nonatomic,strong) NSMutableArray *underlineViewsArr;
//@property (nonatomic,strong) NSMutableArray *widthArr;
@property (nonatomic,assign) NSUInteger selectIndex;
@property (nonatomic,strong) UIColor *textColor;
@property (nonatomic,strong) UIColor *selectTextColor;
@property (nonatomic,assign) long totalWidth;
@property (nonatomic,strong) UIColor *underlineColor;
@property (nonatomic, assign) BOOL isLoad;
@end

@implementation BGCommonScrollToolPanel

//+ (instancetype)createPanelWithFrame:(CGRect)frame TitleArr:(NSArray *)titleArr
//{
//    return [[self alloc] initFramPanelWithTitleArr:titleArr];
//}

- (instancetype)initWithFrame:(CGRect)frame underlineColor:(UIColor *)underlineColor TitleArr:(NSMutableArray *)titleArr textColor:(UIColor *)textColor selectTextColor:(UIColor *)selectTextColor
{
    self.backgroundColor = [UIColor clearColor];
    self = [super initWithFrame:frame];
    if (self)
    {
        [self initSubPanelWithTitleArr:titleArr underlineColor:underlineColor textColor:textColor selectTextColor:selectTextColor];
    }
    return self;
}

- (long )getSizeOfString:(NSString *)string{
    NSDictionary *attributes = @{NSFontAttributeName : [UIFont systemFontOfSize:15]};     //字型屬性,設定字型的font
    CGSize maxSize = CGSizeMake(MAXFLOAT, MAXFLOAT);     //設定字元串的寬高  MAXFLOAT為最大寬度極限值  30為固定高度
    CGSize size = [string boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
    long width = (long)(size.width + 0.5);
    return width;     //此方法結合  預編譯字元串  字型font  字元串寬高  三個參數計算文本  傳回字元串寬度
}


- (long)getViewRectWithTitleArr:(NSArray *)titleArr
{
    long totalWidth = 0;
    _titleArr = [NSMutableArray array];
    self.underlineViewsArr = [NSMutableArray array];
//    self.widthArr = [NSMutableArray array];
    self.titleBtnsArr = [NSMutableArray array];
    self.titleBtnWidthArr = [NSMutableArray array];
    for(NSString *title in titleArr)
    {
        if(!isCommonUnitEmptyString(title))
        {
            long width = [self getSizeOfString:title];
            if(width < 30)
            {
                width = 30;
            }
            if(0 == totalWidth)
            {
                totalWidth = width + TOOL_TITLE_LEFT_Margin*2;
            }
            else
            {
                totalWidth = totalWidth + width + TOOL_TITLE_INTERVAL;
            }
            [self.titleBtnWidthArr addObject:[NSString stringWithFormat:@"%ld",width]];
            [self.titleArr addObject:title];
        }
    }
    self.totalWidth = totalWidth;
    return totalWidth;
}


- (void)initSubPanelWithTitleArr:(NSMutableArray *)titleArr underlineColor:(UIColor *)underlineColor textColor:(UIColor *)textColor selectTextColor:(UIColor *)selectTextColor
{
    self.textColor = textColor;
    self.selectTextColor = selectTextColor;
    self.underlineColor = underlineColor;
    self.titleArr = titleArr;
}

-(void)setTitleArr:(NSMutableArray *)titleArr
{
    if((titleArr == nil) || ![titleArr isKindOfClass:[NSArray class]] || (titleArr.count == 0) || self.isLoad)
    {
        return;
    }
    self.isLoad = YES;
    self.totalWidth = [self getViewRectWithTitleArr:titleArr];
    //大背景
//    self.alertBackgroundView = [[UIScrollView alloc] init];
    self.alertBackgroundView = [[UIScrollView alloc] initWithFrame:self.frame];
    self.alertBackgroundView.backgroundColor = [UIColor clearColor];
    [self addSubview:self.alertBackgroundView];
    self.alertBackgroundView.delegate = self;
    self.alertBackgroundView.pagingEnabled = YES;
    self.alertBackgroundView.bounces = NO;
    self.alertBackgroundView.showsHorizontalScrollIndicator = NO;
    self.alertBackgroundView.scrollsToTop = NO;
    self.alertBackgroundView.contentSize = CGSizeMake(self.totalWidth, self.frame.size.height);
    
//    self.alertBackgroundView.userInteractionEnabled = YES;
//    self.painRecognize1 = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pain1Action:)];
//    self.painRecognize1.maximumNumberOfTouches = 1;
//    [self.alertBackgroundView addGestureRecognizer:self.painRecognize1];
    
    self.selectIndex = 0;
    for (NSInteger index = 0; index < self.titleArr.count; index++)
    {
        NSString *titleStr = [self.titleArr objectAtSafeIndex:index];
        NSLog(@"titleStr=%@", titleStr);
        UIButton *titleBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        titleBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
        titleBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        titleBtn.backgroundColor = [UIColor clearColor];
        [titleBtn setTitle:titleStr forState:UIControlStateNormal];
        titleBtn.tag = index;
        [self.alertBackgroundView addSubview:titleBtn];
        [self.titleBtnsArr addSafeObject:titleBtn];
        [titleBtn addTarget:self action:@selector(hitAction:) forControlEvents:UIControlEventTouchUpInside];
        
        //分隔橫線
        UIView *underlineView = [UIView new];
        underlineView.backgroundColor = self.underlineColor;
        [self.alertBackgroundView addSubview:underlineView];
        [self.underlineViewsArr addSafeObject:underlineView];
        if(0 == index)
        {
            [titleBtn setTitleColor:self.selectTextColor forState:UIControlStateNormal];
            underlineView.hidden = NO;
        }
        else
        {
            [titleBtn setTitleColor:self.textColor forState:UIControlStateNormal];
            underlineView.hidden = YES;
        }
    }
    
//    //分隔橫線
//    self.lineView = [UIView new];
//    self.lineView.backgroundColor = BACKGROUND_COLOR;
//    [self.alertBackgroundView addSubview:self.lineView];
    
    [self unitsSdLayout];
}

- (void)hitAction:(UIButton *)sender {
    if(self.isSendindRequest)
    {
        return;
    }
    NSUInteger index = sender.tag;
    if(index == self.selectIndex)
    {
        return;
    }
    [self updateBtnWithIndex:index];
    if (self.hitCallback) {
        self.hitCallback(sender.tag);
    }
}

-(void)updateBtnWithIndex:(NSUInteger)index
{
    self.selectIndex = index;
    for(NSInteger i = 0; i < self.titleBtnsArr.count; i++)
    {
        UIButton *titleBtn = [self.titleBtnsArr objectAtSafeIndex:i];
        UIView *underlineView = [self.underlineViewsArr objectAtSafeIndex:i];
        if(i != index)
        {
            [titleBtn setTitleColor:self.textColor forState:UIControlStateNormal];
            underlineView.hidden = YES;
        }
        else
        {
            [titleBtn setTitleColor:self.selectTextColor forState:UIControlStateNormal];
            underlineView.hidden = NO;
        }
    }
    

}

-(void)unitsSdLayout
{
    
    for(NSInteger i = 0; i < self.titleBtnsArr.count; i++)
    {
        UIButton *btn = [self.titleBtnsArr objectAtSafeIndex:i];
        CGFloat titleBtnWidth = [[self.titleBtnWidthArr objectAtSafeIndex:i] floatValue];
        if(i == 0)
        {
            btn.sd_layout
            .leftSpaceToView(self.alertBackgroundView, TOOL_TITLE_LEFT_Margin)
            .topSpaceToView(self.alertBackgroundView, (self.frame.size.height - 14 -5 -2)/2)
            .widthIs(titleBtnWidth)
            .heightIs(14); // 設定高度限制
        }
        else
        {
            UIButton *frontBtn = [self.titleBtnsArr objectAtSafeIndex:i-1];
            btn.sd_layout
            .leftSpaceToView(frontBtn, TOOL_TITLE_INTERVAL)
            .centerYEqualToView(frontBtn)
            .widthIs(titleBtnWidth)
            .heightIs(14); // 設定高度限制
        }
        UIView *underlineView = [self.underlineViewsArr objectAtSafeIndex:i];
        underlineView.sd_layout
        .centerXEqualToView(btn)
        .topSpaceToView(btn, 5)
        .widthIs(30)
//        .leftSpaceToView(btn, -3 - titleBtnWidth)
//        .bottomSpaceToView(self.lineView, 0)
//        .widthIs(6+titleBtnWidth)
        .heightIs(2); // 設定高度限制
    }
}      
if(self.commonScrollToolPanel)
        {
//            self.commonScrollToolPanel.titleArr = self.model.cateToolEntity.titleArr;
            return self.commonScrollToolPanel;
        }
        BGCommonScrollToolPanel *sectionHeadView = [[BGCommonScrollToolPanel alloc] initWithFrame:CGRectMake(0, 0, kUIScreenWidth, (40 +15)) underlineColor:BGColorHex(FF7648) TitleArr:self.model.cateToolEntity.titleArr textColor:RGB(51, 51, 51) selectTextColor:RGB(51, 51, 51)];
        @weakify(self);
        sectionHeadView.hitCallback = ^(NSInteger index) {
            @strongify(self);
            BGCateToolUnitEntity *selectCateToolUnitEntity = [self.model.cateToolEntity.cate_list bitobjectOrNilAtIndex:index];
            if(selectCateToolUnitEntity && [selectCateToolUnitEntity isKindOfClass:[BGCateToolUnitEntity class]] && !isCommonUnitEmptyString(selectCateToolUnitEntity.cate_id))
            {
                self.model.selectCateToolUnitEntity = selectCateToolUnitEntity;
                [self loadNewData];
            }
        };
        self.commonScrollToolPanel = sectionHeadView;
        return sectionHeadView;