图形控件

UICollectionViewFlowLayout自定义布局

2017-08-28  本文已影响0人  怪兽密保

UICollectionViewFlowLayout的不同布局方式

1.上一view下六个view模式


//上一下六模式
- (void)layoutAttributesForServiceLayout:(UICollectionViewLayoutAttributes *)layoutAttributes indexPath: (NSIndexPath *) indexPath {
    CGFloat y = self.contentHeight;
    if (indexPath.item == 0) {
        layoutAttributes.frame = CGRectMake(0, y, SCREEN_WIDTH, kBaseLine(85));
        self.contentHeight += kBaseLine(85);
    } else {
        if (indexPath.item > 6) { return; }
        long row = (indexPath.item -1) % 3;
        CGFloat width = SCREEN_WIDTH / 3.0;
        layoutAttributes.frame = CGRectMake(row * width, y, width, kBaseLine(100));
        if (indexPath.item == 3 || indexPath.item == [self.collectionView numberOfItemsInSection:indexPath.section] - 1) {
            self.contentHeight += kBaseLine(100);
        }
    }
}

2.左边一个右边两个布局模式

//左一右二模式
- (void)layoutAttributesForCopyRightlayout:(UICollectionViewLayoutAttributes *)layoutAttributes indexPath: (NSIndexPath *) indexPath {
    CGFloat y = self.contentHeight;
    CGFloat width = SCREEN_WIDTH / 2.0;
    CGFloat height = kBaseLine(160);
    switch (indexPath.item) {
        case 0:
            layoutAttributes.frame = CGRectMake(0, y, width, height);
            break;
        case 1:
            layoutAttributes.frame = CGRectMake(width, y, width, height / 2.0);
            break;
        case 2:
            layoutAttributes.frame = CGRectMake(width, (height / 2.0) + y, width, height / 2.0);
            break;
        default:
            break;
    }
    if (indexPath.item == [self.collectionView numberOfItemsInSection:indexPath.section] - 1) {
        self.contentHeight += height;
    }
}

3.上一下二模式

//上一下二模式
- (void)layoutAttributesForPatentLayout:(UICollectionViewLayoutAttributes *)layoutAttributes indexPath: (NSIndexPath *) indexPath {
    CGFloat y = self.contentHeight;
    CGFloat height = 0;
    switch (indexPath.item) {
        case 0:
            layoutAttributes.frame = CGRectMake(0, y, SCREEN_WIDTH, kBaseLine(85));
            height = kBaseLine(85);
            break;
        case 1:
            layoutAttributes.frame = CGRectMake(0, y, SCREEN_WIDTH/2.0, kBaseLine(80));
            height = kBaseLine(80);
            break;
        case 2:
            layoutAttributes.frame = CGRectMake(SCREEN_WIDTH/2.0, y, SCREEN_WIDTH/2.0, kBaseLine(80));
            height = kBaseLine(80);
            break;
        default:
            break;
    }
    if (indexPath.item == 0 || indexPath.item == [self.collectionView numberOfItemsInSection:indexPath.section] - 1) {
        self.contentHeight += height;
    }
}

4.左二右一模式

//左二右一模式
- (void)layoutAttributesForCaseLayout:(UICollectionViewLayoutAttributes *)layoutAttributes indexPath: (NSIndexPath *) indexPath {
    CGFloat y = self.contentHeight;
    switch (indexPath.item) {
        case 0:
            layoutAttributes.frame = CGRectMake(0, y, SCREEN_WIDTH/2.0, kBaseLine(80));
            self.contentHeight += kBaseLine(80);
            break;
        case 1:
            layoutAttributes.frame = CGRectMake(0, y, SCREEN_WIDTH/2.0, kBaseLine(80));
            self.contentHeight += kBaseLine(80);
            break;
        case 2:
            layoutAttributes.frame = CGRectMake(SCREEN_WIDTH/2.0, y - kBaseLine(160), SCREEN_WIDTH/2.0, kBaseLine(160));
            break;
        default:
            break;
    }
}

5.瀑布流模式

- ( void)layoutAttributesForWaterFlowt:(UICollectionViewLayoutAttributes *)layoutAttributes indexPath: (NSIndexPath *) indexPath
{
    // 创建布局属性
//    UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    
    // collectionView的宽度
    CGFloat collectionViewW = self.collectionView.frame.size.width;
    
    // 设置布局属性的frame,取出每列的宽度
    CGFloat w = (collectionViewW - self.edgeInsets.left - self.edgeInsets.right - (self.columnCount - 1) * self.columnMargin) / self.columnCount;
    //取出每列的高度
    CGFloat h = [self.delegate waterflowLayout:WaterflowLayout heightForItemAtIndex:indexPath.item itemWidth:w];
    
    // 找出高度最短的那一列
    NSInteger destColumn = 0;
    CGFloat minColumnHeight = [self.columnHeights[0] doubleValue];
    
    for (NSInteger i = 1; i < self.columnCount; i++) {
      
       
      
        // 取得第i列的高度
        CGFloat columnHeight = [self.columnHeights[i] doubleValue];
        //        比较取出最短那列
      
        if (minColumnHeight > columnHeight) {
            minColumnHeight = columnHeight;
            destColumn = i;
        }
    }
    
 
    CGFloat x = self.edgeInsets.left + destColumn * (w + self.columnMargin);
    //    最短那列的y值
    CGFloat y = minColumnHeight;
   
    if (y != self.edgeInsets.top) {//如果不是第一列就添加行距
        y += self.rowMargin;
       
    }else
    {
        y +=self.totalHeight-self.rowMargin;
     
    }
    layoutAttributes.frame = CGRectMake(x, y, w, h);
    
    // 更新最短那列的高度,已有新的item添加到该列,所以要更新该列的高度
    self.columnHeights[destColumn] = @(CGRectGetMaxY(layoutAttributes.frame));
    
    // 记录内容最高的高度,用最高的高度为CollectionView的内容高度
    CGFloat columnHeight = [self.columnHeights[destColumn] doubleValue];
    if (self.contentHeight < columnHeight) {
        self.contentHeight = columnHeight;
//        self.contentHeight=columnHeight;
    }
//    return attrs;
}

demod地址

上一篇 下一篇

猜你喜欢

热点阅读