天天看点

自定义滑动工具栏

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

UICollectionView的布局要这样设置:​​

​flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;​

​。

#import
#import
//#import "CBPSpecListUnitEntity.h"
#import

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

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