自定义滑动工具栏

简介: 自定义滑动工具栏

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

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
目录
相关文章
|
4月前
|
前端开发 JavaScript
基于Vue3实现鼠标按下某个元素进行拖动,实时改变左侧或右侧元素的宽度,以及点击收起或展开的功能
本文介绍了如何在Vue3项目中实现一个鼠标拖动调整元素宽度的功能,并展示了点击按钮收起或展开侧边栏的效果,提供了完整的实现代码和操作演示。
843 0
基于Vue3实现鼠标按下某个元素进行拖动,实时改变左侧或右侧元素的宽度,以及点击收起或展开的功能
|
7月前
滑动工具条
滑动工具条
50 0
|
JavaScript
46EasyUI 窗口- 自定义窗口工具栏
46EasyUI 窗口- 自定义窗口工具栏
62 0
|
7月前
|
JavaScript
|
7月前
|
Windows
[Qt5] 创建菜单栏、工具栏、核心控件和浮动窗口
[Qt5] 创建菜单栏、工具栏、核心控件和浮动窗口
117 0
|
小程序 JavaScript
小程序底部动画弹框
小程序底部动画弹框
80 0
|
JavaScript 前端开发
49EasyUI 窗口- 自定义带有工具条和按钮的对话框
49EasyUI 窗口- 自定义带有工具条和按钮的对话框
48 0
|
前端开发 JavaScript
两种方式实现css取消页面鼠标双击选中文字或单击拖动选中文字的效果
两种方式实现css取消页面鼠标双击选中文字或单击拖动选中文字的效果
555 0
|
C++ Windows
2.1 窗口和控件
2.1 窗口和控件
2.1 窗口和控件