循环滑动的工具条

简介: 循环滑动的工具条

滑动工具条一般有两种:

一:左右滑动,有左右边界,无法循环滑动的工具条。

二:左右循环滑动的工具条。

咱们使用的大部分是第一类非循环的工具条。

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;
目录
相关文章
|
C#
WPF 左键单击弹出菜单 ContextMenu
原文:WPF 左键单击弹出菜单 ContextMenu WPF中的ContextMenu在XAML中可直接做出来,但是仅限于右键弹出菜单,如果需要添加左键弹出功能,只需要在事件中添加Click事件 XMAL代码如下 ...
2910 0
|
5月前
|
开发框架 数据可视化 C#
|
7月前
滑动工具条
滑动工具条
54 0
|
7月前
[Qt5] 右键窗体弹出菜单,实现图像适应窗体大小
[Qt5] 右键窗体弹出菜单,实现图像适应窗体大小
126 0
CDialog中使用工具栏和状态栏
CDialog中使用工具栏和状态栏
115 0
UGUI系列-鼠标移动到按钮上显示信息(Unity3D)
有时候图标不能很好的说明这个功能的解释,就需要一些说明性文字显示。就比如可以在鼠标移动到UI上面的时候显示文字。 那么如何在UGUI上,鼠标移动上去显示文字说明呢。 大家都知道,当鼠标移动到button按钮上面的时候会出现变化,主要是button这个组件在控制
|
索引 Windows 程序员