循环滑动的工具条
滑动工具条一般有两种: 一:左右滑动,有左右边界,无法循环滑动的工具条。 二:左右循环滑动的工具条。 咱们使用的大部分是第一类非循环的工具条。 BGCateToolEntity.h
#import#import@interface BGCateToolView : BGBaseView@property (nonatomic, strong) BGCateToolUnitEntity *selectCateToolUnitEntity;@property (nonatomic, strong) BGCateToolEntity *model;@property (nonatomic, copy) void (^selectBlock)(BGCateToolUnitEntity *selectCateToolUnitEntity);
BGCateToolView.h
#import#import#import@interface BGCateToolView ()@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];}
BGCateToolUnitEntity.h
#import@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;
使用的示例:
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;
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
暂时没有评论,来抢沙发吧~