瀑布流的实现

简介:瀑布流的实现前提是后台给返回图片的高度宽度

效果图

瀑布流.gif

1.先给大家讲讲怎么使用这个封装

  • 1.瀑布流主要是layout的布局
    你可以把类CWWaterViewLayout.h拖走,里面有几个属性
  /**
   *  collectionView上下左右之间的间距
   */
  @property(nonatomic,assign) UIEdgeInsets sectionInset;

  /**
   *  每一列之间的间距
   */
  @property(nonatomic,assign) CGFloat columnMargin1;

  /**
   *  每一行之间的间距
   */
  @property(nonatomic,assign) CGFloat rowMargin1;

  /**
   *  告诉外界你想显示多少列
   */
  @property(nonatomic,assign) int columnsCount;

默认情况下全是10

  • 2.cell的布局自己设计了

  • 3.传比例(这个方法需要实现)

    #pragma mark - <CWWaterViewLayoutDelegate>的代理方法
    -(CGFloat)waterflowLayout:(CWWaterViewLayout *)waterViewLayout heightForWidth:(CGFloat)width atIndexPath:(NSIndexPath *)indexPath
    {
       ShopModel *shopmodel = self.tempArray[indexPath.item];
       /**
        *  取出来宽高
        */
      NSString *shopH = [NSString stringWithFormat:@"%@",shopmodel.H];
      NSString *shopW = [NSString stringWithFormat:@"%@",shopmodel.W];
      /**
        *  转化为float类型
        */
      CGFloat shopHValue = [shopH floatValue];
      CGFloat shopWValue = [shopW floatValue];
      /**
       *  返回比例计算的高度
       */
      return shopHValue / shopWValue * width;
    }
    

2.重要的是CWWaterViewLayout.m里面的布局计算才是瀑布流的核心代码

#pragma mark5.  尺寸的计算
-(UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
/**
 *  假设最短的那一列是第0列
 */
__block NSString *minYColun = @"0";

[self.maxYdict enumerateKeysAndObjectsUsingBlock:^(NSString *column,NSNumber *maxY, BOOL * _Nonnull stop) {
    
    if ([maxY floatValue] < [self.maxYdict[minYColun] floatValue] ) {
        
        minYColun = column;
    }
}];
/**
 *  计算宽度
 */
CGFloat width =  (self.collectionView.frame.size.width - self.sectionInset.left - self.sectionInset.right - (self.columnsCount - 1) * self.columnMargin1)/self.columnsCount;
/**
 *  高度
 */
CGFloat height = [self.delegate waterflowLayout:self heightForWidth:width atIndexPath:indexPath];

/**
 *  计算x和y
 */
CGFloat x = self.sectionInset.left + (width + self.columnMargin1) * [minYColun floatValue];
CGFloat y = [self.maxYdict[minYColun]floatValue] + self.rowMargin1;

/**
 *  更新这一列的最大Y值
 */
  self.maxYdict[minYColun] = @(y + height);

  UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];

  attrs.frame = CGRectMake(x, y, width, height);

  return attrs;
}

后记:具体的您看代码,我都进行了注释

王冲的瀑布流 密码: 4seg

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

相关阅读更多精彩内容

  • 思路 瀑布流核心思想是,每张图片都是从高度最低的一列去拼接,同时图片的尺寸取决于需求,而不是统一的尺寸,进而形成参...
    BrightFuture阅读 477评论 0 0
  • 前言 超简单的瀑布流实现,这里说一下笔者的思路,详细代码在这里 效果演示 实现思路 collectionView能...
    codingZero阅读 9,402评论 13 146
  • 为何我会对瀑布流为这么上进,非要将瀑布流来一次大清洗,非逼得自己将它彻底弄清楚。原因其实也很简单:之前刚出来找工作...
    smooth_lgh阅读 4,982评论 1 5
  • 三种方法实现 一、scrollView做垫子,上面添加多个tableView(不可取); 效率低下,cell不能循...
    SadMine阅读 883评论 0 0
  • 一转眼,寒假就已经过了一半了,再有不久就要开学了。身边的一些朋友开始感叹:这个寒假过的真无聊啊!可是,当我说到:既...
    我是胖少阅读 616评论 2 7

友情链接更多精彩内容