自定义滑动工具栏

简介: 自定义滑动工具栏

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

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
目录
相关文章
|
API C# Windows
Winform控件优化之无边框窗体及其拖动、调整大小和实现最大最小化关闭功能的自定义标题栏效果
Winform中实现无边框窗体只需要设置FormBorderStyle = FormBorderStyle.None,但是无边框下我们需要保留移动窗体、拖拽调整大小、自定义美观好看的标题栏等...
3460 0
Winform控件优化之无边框窗体及其拖动、调整大小和实现最大最小化关闭功能的自定义标题栏效果
|
4月前
|
JavaScript 容器
Vue 动画 —— 滑动切换动画 / 滑动翻页过渡动画——从顶部到底部、从底部到顶部、从左侧到右侧、从右侧到左侧
Vue 动画 —— 滑动切换动画 / 滑动翻页过渡动画——从顶部到底部、从底部到顶部、从左侧到右侧、从右侧到左侧
770 0
|
6月前
滑动工具条
滑动工具条
42 0
|
JavaScript
46EasyUI 窗口- 自定义窗口工具栏
46EasyUI 窗口- 自定义窗口工具栏
59 0
|
6月前
|
JavaScript
|
小程序 JavaScript
小程序底部动画弹框
小程序底部动画弹框
76 0
|
JavaScript 前端开发
49EasyUI 窗口- 自定义带有工具条和按钮的对话框
49EasyUI 窗口- 自定义带有工具条和按钮的对话框
45 0
|
小程序 JavaScript
小程序顶部导航栏,可滑动,可动态选中放大
小程序顶部导航栏,可滑动,可动态选中放大
200 0