zoukankan      html  css  js  c++  java
  • 多个section自定义UICollectionViewLayout,不同宽度的item等间距

    2017-09-28

    - (void)prepareLayout {
    
        [super prepareLayout];
    
        self.lastPoint = CGPointZero;
    
        self.AttrList = [NSMutableArray array];
    
        NSUInteger sectionCount = [self.collectionView numberOfSections];
    
        for (NSInteger index = 0; index < sectionCount; index++) {
    
            NSInteger hotItemCount = [self.collectionView numberOfItemsInSection:index];
    
            if (index != sectionCount-1) {
    
                //最后一个区不需header
    
                [self.AttrList addObject:[self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader atIndexPath:[NSIndexPath indexPathForItem:0 inSection:index]]];
    
            }
    
            for (int i =0; i <hotItemCount; i++) {
    
                UICollectionViewLayoutAttributes *attributes = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:i inSection:index]];
    
                [self.AttrList addObject:attributes];
    
            }
    
            UICollectionViewLayoutAttributes *hotLastAttributes = self.AttrList.lastObject;
    
            self.lastPoint = CGPointMake(0, hotLastAttributes.frame.size.height +hotLastAttributes.frame.origin.y +[self itemVerticalPadding]);
    
        }
    
    }
    
     
    
    - (CGSize)collectionViewContentSize {
    
        return CGSizeMake(SCREENWIDTH-20, self.lastPoint.y);
    
    }
    
     
    
    - (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
    
        return [self.AttrList filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(UICollectionViewLayoutAttributes *evaluatedObject, NSDictionary *bindings) {
    
            return CGRectIntersectsRect(rect, [evaluatedObject frame]);
    
        }]];
    
    }
    
     
    
     
    
    - (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath {
    
        UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind:elementKind withIndexPath:indexPath];
    
        attributes.frame = CGRectMake(0, self.lastPoint.y, SCREENWIDTH-20, _sectionHeaderHeight);
    
        self.lastPoint = CGPointMake(0, self.lastPoint.y +_sectionHeaderHeight);
    
        return attributes;
    
    }
    
     
    
    - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
    
        UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    
        CGFloat itemVerticalPadding = [self itemVerticalPadding];
    
        CGFloat itemHorizontalPadding = [self itemHorizontalPadding];
    
        CGSize itemTextSize = [self.delegate itemTextSizeIndexPath:indexPath];
    
        CGFloat itemWidth = itemTextSize.width +staticItemHotInsets.left +staticItemHotInsets.right;
    
        CGFloat itemHeight = itemTextSize.height +staticItemHotInsets.top +staticItemHotInsets.bottom;
    
        if (self.lastPoint.x +itemHorizontalPadding +itemWidth >=SCREENWIDTH-20) {
    
            UICollectionViewLayoutAttributes *lastAttributes = self.AttrList.lastObject;
    
            self.lastPoint = CGPointMake(0, lastAttributes.frame.size.height +lastAttributes.frame.origin.y);
    
        }
    
        CGRect rect = CGRectMake(self.lastPoint.x +itemHorizontalPadding, self.lastPoint.y +itemVerticalPadding, itemWidth, itemHeight);
    
        attributes.frame = rect;
    
        self.lastPoint = CGPointMake(rect.origin.x +rect.size.width, self.lastPoint.y);
    
        return attributes;
    
    }
    
     
    
    - (CGFloat)itemHorizontalPadding {
    
        if (self.delegate && [self.delegate respondsToSelector:@selector(customItemHorizontalPadding)])
    
            return [self.delegate customItemHorizontalPadding];
    
        return 10;
    
    }
    
     
    
    - (CGFloat)itemVerticalPadding {
    
        if (self.delegate && [self.delegate respondsToSelector:@selector(customItemVerticalPadding)])
    
            return [self.delegate customItemVerticalPadding];
    
        return 10;
    
    }
    
     
    
    - (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
    
    {
    
        return NO;
    
    }
  • 相关阅读:
    [其他]将Windows Terminal添加到右键菜单
    [VS Code]在自己的Ubuntu服务器上构建VSCode Online
    [Go]goFileView-基于Golang的在线Office全家桶预览
    [Go]基于Go语言的Web路由转发,多个网站共享一个端口(新版本,支持WebSocket)
    [WSL]在Windows10子系统里安装运行桌面(xUbuntu)
    [Go]使用Golang对鸢尾花数据集进行k-means聚类
    [Python+JavaScript]JS调用摄像头并拍照,上传至tornado后端并转换为PIL的Image
    [Python]Python基于OpenCV批量提取视频中的人脸并保存
    [WSL]Windows10 Ubuntu子系统编译安装线程安全版LAMP
    [Go]基于Go语言的Web路由转发,多个网站共享一个端口(存在问题,已经抛弃,新解决方案请看新博客)
  • 原文地址:https://www.cnblogs.com/idefei/p/7607201.html
Copyright © 2011-2022 走看看