自定义滑动工具栏

简介: 自定义滑动工具栏

根据内容显示左右带固定宽度边距和背景的标签。

UICollectionView的布局要这样设置:flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;。


BGCommonSlideCollectionCell.h

#import <UIKit/UIKit.h>
#import "PPCollectionViewCell.h"
//#import "CBPSpecListUnitEntity.h"
#import "CBPSpecListUnitEntity.h"

@interface BGCommonSlideCollectionCell : PPCollectionViewCell
//@property (nonatomic, strong) BGCalendarHeadView *calendarHeadView;
//
@property (nonatomic, strong) CBPItemListUnitEntity *selectItemListUnitEntity;
//@property (nonatomic, assign) BOOL isShowToday;
//@property (nonatomic, assign) BOOL isUnfold;
@property (nonatomic, strong) CBPSpecListUnitEntity *model;

//@property (nonatomic, copy) void(^hitBlock)(NSUInteger year, NSUInteger month, NSUInteger day);
@property (nonatomic, copy) void (^selectBlock)(CBPItemListUnitEntity *selectItemListUnitEntity,CBPSpecListUnitEntity *specListUnitEntity);

//- (void)updateWithSelectCateToolUnitEntity:(BGCateToolUnitEntity *)selectCateToolUnitEntity model:(CBPSpecListUnitEntity *)model;
@end

BGCommonSlideCollectionCell.m

#import "BGCommonSlideCollectionCell.h"
#import "BGCalendarFootView.h"
#import "CBPItemListCollectionCell.h"

@interface BGCommonSlideCollectionCell ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout,UIGestureRecognizerDelegate>
//@property (nonatomic, assign) BOOL isEnabledSelect;
//@property (nonatomic, assign) BOOL isHiddenLine;
//@property (nonatomic, strong) UIColor *selectColor;
//@property (nonatomic, strong) UIView *roundView;
@property (nonatomic, strong) UIView *bigBackgroundView;
//@property (nonatomic, strong) UIColor *lineColor;
//@property (nonatomic, strong) UIImage *icon;
//@property (nonatomic, strong) UIImage *selectedIcon;
//
//
//
//@property (nonatomic, strong) BGCalendarFootView *calendarFootView;
@property (nonatomic, strong) UICollectionView *collectionView;
//@property (nonatomic, strong) UICollectionReusableView *headView;
//@property (nonatomic, strong) UICollectionReusableView *collectionHeadView;
//@property (nonatomic, strong) UICollectionReusableView *parentCoursesCollectionHeadView;
//@property (nonatomic, strong) UICollectionReusableView *footmarkHeadView;
//@property (nonatomic, strong) UICollectionReusableView *lineFootView;
//
//@property (nonatomic,strong) UIPanGestureRecognizer *painRecognize1;
//@property (nonatomic, assign) float shift;
//@property (nonatomic, assign) BOOL isPanGestureRecognizer;
@end

@implementation BGCommonSlideCollectionCell

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

-(void)setup
{
    self.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.collectionView];
}


- (UICollectionView *)collectionView {
    if(!_collectionView)
    {
        //创建布局
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];

        //创建CollectionView
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(15, 0, sCommonUnitFullWidth()-15, self.frame.size.height) collectionViewLayout:flowLayout];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.alwaysBounceVertical = NO;
        if (@available(iOS 11.0, *)) {
            _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        _collectionView.backgroundColor = [UIColor whiteColor];//RGBA(246, 246, 246, 1);//BGColorHex(F9F9F9);
//        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"lineFootView"];
        [_collectionView registerClass:[CBPItemListCollectionCell class] forCellWithReuseIdentifier:NSStringFromClass([CBPItemListCollectionCell class])];

        //定义每个UICollectionView 的大小
//        flowLayout.itemSize = CGSizeMake(80 , 20);
        //定义每个UICollectionView 横向的间距
//        flowLayout.minimumLineSpacing = 10;
//        //定义每个UICollectionView 纵向的间距
//        flowLayout.minimumInteritemSpacing = 10;
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

        //定义每个UICollectionView 的边距距
        //    flowLayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);//上左下
    }
    return _collectionView;
}

//两个cell之间的间距(同一行的cell的间距)

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}

//这个是两行cell之间的间距(上下行cell的间距
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CBPItemListCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([CBPItemListCollectionCell class]) forIndexPath:indexPath];
    cell.model = [self.model.itemList bitobjectOrNilAtIndex:indexPath.row];
//    [cell updateWithselectItemListUnitEntity:self.selectItemListUnitEntity model:[self.model.cate_list bitobjectOrNilAtIndex:indexPath.row]];
    return  cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.model.itemList.count;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    self.selectItemListUnitEntity = [self.model.itemList bitobjectOrNilAtIndex:indexPath.row];
    if(self.selectItemListUnitEntity && [self.selectItemListUnitEntity isKindOfClass:[CBPItemListUnitEntity class]] && self.selectBlock)
    {
        self.selectItemListUnitEntity.isSelect = YES;
        self.selectBlock(self.selectItemListUnitEntity, self.model);
        [self.collectionView reloadData];
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CBPItemListUnitEntity *entity = [self.model.itemList bitobjectOrNilAtIndex:indexPath.row];
    if(entity && [entity isKindOfClass:[CBPItemListUnitEntity class]])
    {
        return CGSizeMake(entity.width, 32);
    }
    return CGSizeMake(75, 32);
}

//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
//    return CGSizeMake(FULL_WIDTH, BG_1PX);
//}


创建头视图
//- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
//{
//    if (self.lineFootView)
//    {
//        return self.lineFootView;
//    }
//    
//    UICollectionReusableView *lineFootView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader
//                                                                                withReuseIdentifier:@"lineFootView" forIndexPath:indexPath];
//    self.lineFootView = lineFootView;
//    UIView *lineFootViewPanel = [[UIView alloc] initWithFrame:CGRectMake(0, 0, FULL_WIDTH, BG_1PX)];
//    lineFootViewPanel.backgroundColor = [UIColor clearColor];
//    [lineFootView addSubview:lineFootViewPanel];
//    return lineFootView;
//}


//- (CBPSpecListUnitEntity *)model {
//    if(!_model)
//    {
//        _model = [[CBPSpecListUnitEntity alloc] init];
//    }
//    return _model;
//}

//- (void)updateWithselectItemListUnitEntity:(CBPItemListUnitEntity *)selectItemListUnitEntity model:(CBPSpecListUnitEntity *)model
//{
//    self.selectItemListUnitEntity = selectItemListUnitEntity;
//    self.model = model;
//}

-(void)setModel:(CBPSpecListUnitEntity *)model
{
    if(!model || ![model isKindOfClass:[CBPSpecListUnitEntity class]])
    {
        return;
    }
    _model = model;
//    self.selectItemListUnitEntity = [self.model.cate_list bitobjectOrNilAtIndex:0];
    [self.collectionView reloadData];
}

@end
目录
相关文章
|
Linux KVM 虚拟化
云计算——ACA学习 虚拟化技术产品介绍
云计算——ACA学习 虚拟化技术产品介绍
324 0
|
8月前
|
人工智能 监控 数据安全/隐私保护
AI视频监控在大型商场的隐私保护技术
为保障隐私合规,商场采取数据加密与匿名化处理,防止敏感信息泄露;同时通过透明性声明和合法授权,确保顾客知情并同意监控措施。技术手段包括加密算法保护、去除身份识别细节,并在显眼位置张贴隐私政策,采用电子屏幕、语音提示或二维码获取顾客同意,确保监控行为合法合规。
253 0
|
Java 关系型数据库 MySQL
实时计算 Flink版产品使用问题之如何实现MySQL数据库之间的实时同步
实时计算Flink版作为一种强大的流处理和批处理统一的计算框架,广泛应用于各种需要实时数据处理和分析的场景。实时计算Flink版通常结合SQL接口、DataStream API、以及与上下游数据源和存储系统的丰富连接器,提供了一套全面的解决方案,以应对各种实时计算需求。其低延迟、高吞吐、容错性强的特点,使其成为众多企业和组织实时数据处理首选的技术平台。以下是实时计算Flink版的一些典型使用合集。
|
Java Go
Golang深入浅出之-Goroutine泄漏检测与避免:pprof与debug包
【5月更文挑战第1天】本文介绍了Go语言中goroutine泄漏的问题及其影响,列举了忘记关闭通道、无限循环和依赖外部条件等常见泄漏原因。通过引入`net/http/pprof`和`runtime/debug`包,可以检测和避免goroutine泄漏。使用pprof的HTTP服务器查看goroutine堆栈,利用`debug`包的`SetGCPercent`和`FreeOSMemory`函数管理内存。实践中,应使用`sync.WaitGroup`、避免无限循环和及时关闭通道来防止泄漏。理解这些工具和策略对维护Go程序的稳定性至关重要。
346 4
|
C# 数据库
C#中的空合并运算符与空合并赋值运算符:简化空值处理
C#中的空合并运算符与空合并赋值运算符:简化空值处理
189 0
kali安装vmtools
kali安装vmtools
394 0
|
存储 Java Spring
Spring框架中的Resource接口是什么,以及它在加载和访问资源时的关键作用
使用 Resource 加载资源 要使用 Resource 接口加载资源,首先需要获取一个 ResourceLoader 实例,通常可以通过依赖注入来获得。接下来,您可以使用 ResourceLoader 来获取 Resource 对象,然后使用它来访问资源的内容。
311 0
|
索引 Windows
CocosCreator3.8研究笔记(十二)CocosCreator 字体资源理解
CocosCreator3.8研究笔记(十二)CocosCreator 字体资源理解
808 0
|
索引 Python
python基础回顾
python基础回顾
106 0