滑动工具条一般有两种:
一:左右滑动,有左右边界,无法循环滑动的工具条。
二:左右循环滑动的工具条。
咱们使用的大部分是第一类非循环的工具条。
BGCateToolEntity.h
#import <UIKit/UIKit.h> #import "BGCateToolEntity.h" @interface BGCateToolView : BGBaseView @property (nonatomic, strong) BGCateToolUnitEntity *selectCateToolUnitEntity; @property (nonatomic, strong) BGCateToolEntity *model; @property (nonatomic, copy) void (^selectBlock)(BGCateToolUnitEntity *selectCateToolUnitEntity); @end
BGCateToolView.h
#import "BGCateToolView.h" #import "BGCalendarFootView.h" #import "BGCateToolCollectionCell.h" @interface BGCateToolView ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout,UIGestureRecognizerDelegate> @property (nonatomic, strong) UIView *bigBackgroundView; @property (nonatomic, strong) UICollectionView *collectionView; @end @implementation BGCateToolView - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self setup]; } return self; } -(void)setup { self.backgroundColor = [UIColor clearColor]; self.bigBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, sCommonUnitFullWidth(), 20+14+2+4+15)]; self.bigBackgroundView.backgroundColor = [UIColor whiteColor]; [self addSubview:self.bigBackgroundView]; [self.bigBackgroundView addSubview:self.collectionView]; } - (UICollectionView *)collectionView { if(!_collectionView) { //创建布局 UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init]; //创建CollectionView _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(15, 20, sCommonUnitFullWidth()-15, 14+2+4) 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:[BGCateToolCollectionCell class] forCellWithReuseIdentifier:NSStringFromClass([BGCateToolCollectionCell class])]; //定义每个UICollectionView 的大小 flowLayout.itemSize = CGSizeMake(100 , 20); //定义每个UICollectionView 横向的间距 flowLayout.minimumLineSpacing = BG_1PX; //定义每个UICollectionView 纵向的间距 flowLayout.minimumInteritemSpacing = BG_1PX; 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 BG_1PX; } //这个是两行cell之间的间距(上下行cell的间距 -(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section { return BG_1PX; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ BGCateToolCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([BGCateToolCollectionCell class]) forIndexPath:indexPath]; [cell updateWithSelectCateToolUnitEntity:self.selectCateToolUnitEntity model:[self.model.cate_list bitobjectOrNilAtIndex:indexPath.row]]; return cell; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ return self.model.cate_list.count; } - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return isCommonUnitEmptyArray(self.model.cate_list) ? 0 : self.model.cate_list.count; } -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ self.selectCateToolUnitEntity = [self.model.cate_list bitobjectOrNilAtIndex:indexPath.row]; if(self.selectCateToolUnitEntity && [self.selectCateToolUnitEntity isKindOfClass:[BGCateToolUnitEntity class]] && self.selectBlock) { self.selectBlock(self.selectCateToolUnitEntity); [self.collectionView reloadData]; } } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { return CGSizeMake(100 , 20); } -(void)setModel:(BGCateToolEntity *)model { if(!model || ![model isKindOfClass:[BGCateToolEntity class]]) { return; } _model = model; self.selectCateToolUnitEntity = [self.model.cate_list bitobjectOrNilAtIndex:0]; [self.collectionView reloadData]; } @end
BGCateToolUnitEntity.h
#import <Foundation/Foundation.h> @interface BGCateToolUnitEntity : BGBaseEntity @property (nonatomic, strong) NSString *cate_id; @property (nonatomic, assign) BOOL entity_type; @property (nonatomic, strong) NSString *name; @property (nonatomic, strong) NSString *pid; @end
使用的示例:
if(self.sectionHeadView) { return self.sectionHeadView; } BGCateToolView *sectionHeadView = [[BGCateToolView alloc] initWithFrame:CGRectMake(0, 0, kUIScreenWidth, (40 +15))]; @weakify(self); sectionHeadView.selectBlock = ^(BGCateToolUnitEntity *selectCateToolUnitEntity) { @strongify(self); if(selectCateToolUnitEntity && [selectCateToolUnitEntity isKindOfClass:[BGCateToolUnitEntity class]] && !isCommonUnitEmptyString(selectCateToolUnitEntity.cate_id)) { self.model.selectCateToolUnitEntity = selectCateToolUnitEntity; [self loadNewData]; } }; sectionHeadView.model = self.model.cateToolEntity; self.sectionHeadView = sectionHeadView; return sectionHeadView;