滑动工具条

简介: 滑动工具条

滑动工具条一般有两种:

一:左右滑动,有左右边界,无法循环滑动的工具条。

二:左右循环滑动的工具条。

咱们使用的大部分是第一类非循环的工具条。

BGCommonScrollToolPanel.h

#import <UIKit/UIKit.h>

@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;

@end

BGCommonScrollToolPanel.m

#import "BGCommonScrollToolPanel.h"

#define TOOL_TITLE_LEFT_Margin     15
#define TOOL_UNDERLINE_LEFT_Margin 22
#define TOOL_CHARACTER_WIDTH       15
#define TOOL_TITLE_INTERVAL        20

@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); // 设置高度约束
    }
}

@end

使用的代码示例:

        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;
目录
相关文章
|
编解码
一文详解 URLEncode
使用浏览器进行Http网络请求时,若请求query中包含中文,中文会被编码为 `%+16进制+16进制`形式,但你真的深入了解过,为什么要进行这种转义编码吗?编码的原理又是什么?
2449 0
一文详解 URLEncode
|
机器学习/深度学习 人工智能 负载均衡
MoE架构:大模型的规模扩展革命
MoE(混合专家)架构通过稀疏激活多个专业化子网络,实现高效计算与大规模模型的结合,提升训练推理效率及模型可扩展性,成为大模型发展的重要范式。
|
9月前
|
安全 前端开发 Android开发
原生 APP的开发流程
原生APP(iOS/Swift、Android/Kotlin)开发流程标准化,覆盖需求分析、UI/UX设计、架构搭建、编码、测试、上架合规及迭代运维七大阶段。虽跨端技术流行,但在金融、游戏、系统工具等对性能、硬件调用与安全要求高的领域,原生开发仍是首选。(239字)
|
12月前
|
SQL Oracle 关系型数据库
Oracle 常用函数
拿到手的数据不总是规规矩矩的?Oracle内置了超多实用函数,可以直接在SQL里“加工”数据。无论是大小写转换、截取字符串,还是日期的加减、数字的四舍五入,都有对应的函数。特别推荐用NVL来优雅地处理空值,以及用CASE语句实现复杂的判断逻辑,能让你的查询既强大又省事。
1212 4
|
9月前
|
人工智能 Cloud Native 安全
AWS Bedrock托管Claude 4.6的工程实践与合规思考
近期AWS Bedrock集成Claude 4.6引发热议。该架构以VPC内数据隔离、云原生无缝集成及Firecracker微虚拟机硬隔离为核心,兼顾合规(SOC2/GDPR)、安全与工程效率。国内企业出海需关注主体资质、模型白名单申请及跨境网络优化。
|
消息中间件 Java 测试技术
RocketMQ-5.3.1异常、原因汇总表
本简介汇总了常见的RocketMQ异常信息及其解决方案,涵盖主题配置、网络通信、SSL设置、权限控制、消息发送与消费等多个方面,帮助开发者快速定位和理解异常原因。
1156 86
|
12月前
|
存储 人工智能 安全
医疗影像云存储方案
医疗影像云存储方案通过云原生技术,构建安全合规、高效智能的影像管理新范式。面对大文件、高并发、长周期保存等挑战,方案融合分层架构、分片传输、全链路加密与AI协同,实现弹性扩容、低延迟访问与成本优化,并已在三甲医院成功落地,助力精准医疗迈向高质量发展。(238字)
958 1
|
运维 网络安全 API
SDN 和 NFV:把网络“拆开重做”的底层革命,到底牛在哪?
SDN 和 NFV:把网络“拆开重做”的底层革命,到底牛在哪?
598 4
|
JSON 前端开发 JavaScript
js中await用法
js中await用法
498 0
|
机器学习/深度学习 自然语言处理 监控
命名实体识别(Named Entity Recognition, NER)
命名实体识别(Named Entity Recognition, NER)
1051 7