功能页

#import "RGTypeCollectionViewCell.h"

#define pageNumber      8

@interface FunctionView()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *myCollectionView;
@property (assign, nonatomic) NSInteger functionHeight;
@property (nonatomic, strong) UIPageControl *pageControl;
@property (nonatomic, strong) NSArray *myData;

@end

static NSString *RGTypeCollectionViewCellID = @"RGTypeCollectionViewCellID";

@implementation FunctionView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}
//初始化collectionView
-(void)setupUI{
    //
    if (nil == _myCollectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        flowLayout.minimumLineSpacing = 0;
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        _myCollectionView = [[UICollectionView alloc]initWithFrame:self.bounds collectionViewLayout:flowLayout];
        _myCollectionView.delegate = self;
        _myCollectionView.dataSource = self;
        _myCollectionView.pagingEnabled = YES;
        _myCollectionView.showsVerticalScrollIndicator = NO;
        _myCollectionView.showsHorizontalScrollIndicator = NO;
        _myCollectionView.backgroundColor = [UIColor whiteColor];
        [_myCollectionView registerClass:[RGTypeCollectionViewCell class] forCellWithReuseIdentifier:RGTypeCollectionViewCellID];
        [self addSubview:_myCollectionView];
    }
    if (nil == _pageControl) {
        _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, _myCollectionView.bottom - 20, KScreen_Width, 30)];
        _pageControl.currentPage = 0;
        _pageControl.currentPageIndicatorTintColor = RGB(88, 148, 178);
        _pageControl.pageIndicatorTintColor = RGB(131, 119, 98);
        _pageControl.numberOfPages = self.myData.count / pageNumber + ((self.myData.count % pageNumber) > (0) ? 1 : 0);
        [self addSubview:_pageControl];
    }
}


#pragma mark - UICollectionViewDelegate
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    //计算有多少个,  每行为8个
    return (self.myData.count / pageNumber + ((self.myData.count % pageNumber) > (0) ? 1 : 0)) * pageNumber;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    RGTypeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:RGTypeCollectionViewCellID forIndexPath:indexPath];
    //小于总数赋值, 大于的时候略过赋空
    if (indexPath.item < self.myData.count) {
        RGMoreModel *model = self.myData[indexPath.item];
        [cell textName:model.name imageName:model.icon];
        cell.isShowRed = NO;
    }else{
        [cell textName:@"" imageName:@""];
        cell.isShowRed = NO;
    }
    return cell;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(self.width/4, self.height/2);
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.item < self.myData.count) {
        //选择某个
        if ([self.delegate respondsToSelector:@selector(didSelectIitem:)]) {
            RGMoreModel *model = self.myData[indexPath.item];
            [self.delegate didSelectIitem:model.vcName];
        }
    }
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    CGFloat offsetX = scrollView.contentOffset.x;
    self.pageControl.currentPage = offsetX / KScreen_Width;
}

#pragma mark - setFunctionArr
-(void)setFunctionArr:(NSArray *)functionArr{
    _functionArr = functionArr;
    
    self.myCollectionView.height = self.height;
    [self.myCollectionView reloadData];
}
-(NSArray *)myData{
    if (nil == _myData) {
        _myData = [RGMoreModel MyCollectionModelWithData];
    }
    return _myData;
}

@end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容