美文网首页
CollectionFlowLayout深度定制

CollectionFlowLayout深度定制

作者: 我只是个仙 | 来源:发表于2016-11-24 11:59 被阅读94次

从做海淘项目开始 就想着用flowlayout深度定制布局,一直懒到现在才真正算是懂了一点点皮毛,就在这里记录下来吧。效果如下(暂时还不了解简书如何缩小图片呀):


首先,可以通过xib拖动控件 实现基本控件的分布

1、拖拽一个viewcontroller进来,或者直接用Main.storyboard自带的那个view controller都行

然后拖拽一个collection view 进来 设置约束 如下图:

设置layout依赖

然后依次拖拽collectionviewcell 和imageView,步骤就不赘述了

2、定制flowlayout.h 和.m文件,这里就直接上代码算了

@implementation BookListViewFlowLayout
{
CGFloat _contentWidth;
CGFloat _contentHeight;
CGFloat _commonWidth;
}

////瀑布流布局

- (void)prepareLayout {
    self.cache = ({
    _cache = [[NSMutableArray alloc]init];
    _cache;
  });
  self.numberOfColumns = 3;
  UIEdgeInsets contentInsets = self.collectionView.contentInset;
  _contentWidth = CGRectGetWidth(self.collectionView.bounds) - contentInsets.left - contentInsets.right;
  _contentHeight = 0;
  if (self.cache.isEmpty) {
    CGFloat rowWidth = (_contentWidth)/(CGFloat)self.numberOfColumns;
    CGFloat xOffset[self.numberOfColumns];
    CGFloat yOffset[self.numberOfColumns];
    NSInteger column = 0;
     for (NSInteger i=0; i<self.numberOfColumns;i++){
      CGFloat x = i*rowWidth;
      xOffset[i] = x;
    }
  for (NSInteger j = 0; j < [self.collectionView numberOfItemsInSection:0]; j++) {
  NSIndexPath * indexPath = [NSIndexPath indexPathForItem:j inSection:0];
  CGFloat photoHeight = [self.pinterestLayoutDelegate collectionView:self.collectionView           heightForPhotoAtIndexPath:indexPath];
  CGRect frame = CGRectMake(xOffset[column], yOffset[column], rowWidth, photoHeight);
  UICollectionViewLayoutAttributes * layoutAttribute = [self layoutAttributesForItemAtIndexPath:indexPath];
  layoutAttribute.frame = frame;
  _contentHeight = MAX(_contentHeight, CGRectGetMaxY(frame));
  [self.cache addObject:layoutAttribute];
  yOffset[column] += photoHeight;
  column = [self minYOffset:yOffset];
    }
  }
}
- (NSInteger)minYOffset:(CGFloat *)yOffset {
  NSInteger column = 0;
  for (int i=1; i < self.numberOfColumns; i++) {
  if (yOffset[i] < yOffset[column]) {
    column = i;
    }
  }
return column;
}
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
    NSMutableArray * attriArray = [[NSMutableArray alloc]init];
    for (UICollectionViewLayoutAttributes * subAttribute in self.cache) {
    if (CGRectIntersectsRect(subAttribute.frame, rect)) {
      [attriArray addObject:subAttribute];
    }
  }
  return attriArray;
}
- (CGSize)collectionViewContentSize {
  return CGSizeMake(_contentWidth, _contentHeight);
}

.h文件如下

#define ItemCommonWidth (CGRectGetWidth(self.collectionView.bounds) - self.collectionView.contentInset.left - self.collectionView.contentInset.right);
@protocol PinterestLayoutDelegate- (CGFloat)collectionView:(UICollectionView *)collectionView heightForPhotoAtIndexPath:(NSIndexPath *)indexpath;//- (CGFloat)collectionView:(UICollectionView *)collectionView heightForAnnotationAtIndexPath:(NSIndexPath *)indexpath width:(CGFloat)width;
@end
@interface BookListViewFlowLayout : UICollectionViewFlowLayout@property (nonatomic, assign)NSInteger numberOfColumns;
@property (nonatomic, strong)NSMutableArray * cache;@property (nonatomic, weak)idpinterestLayoutDelegate;
@end

VC代码如下

@interface BookListViewController ()@property (nonatomic, strong)NSArray * photos;
@property (nonatomic, assign)CGFloat lastContentOffset;
@end
@implementation BookListViewController
{
BOOL _scrollToRight;
}
- (void)viewDidLoad {
  [super viewDidLoad];
  self.collectionView.decelerationRate = UIScrollViewDecelerationRateNormal;
  BookListViewFlowLayout * layout = (id)self.collectionView.collectionViewLayout;
  layout.pinterestLayoutDelegate = self;
  layout.minimumLineSpacing = 6;
  self.photos = @[@"IMG_00",@"IMG_01",@"IMG_02",@"IMG_03",@"IMG_04",@"IMG_00"];
  self.collectionView.contentInset = UIEdgeInsetsMake(30, 40, 120, 40);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView heightForPhotoAtIndexPath:(NSIndexPath *)indexpath {
  CGFloat aa =  arc4random()%1000;
  return aa;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  GDCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell"       forIndexPath:indexPath];
  cell.iconImageView.image = [UIImage imageNamed:self.photos[indexPath.item%5]];
  return cell;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  return 100;
}

请原谅我对makdown的无知,还处于小白阶段
I'am GD

相关文章

  • CollectionFlowLayout深度定制

    从做海淘项目开始 就想着用flowlayout深度定制布局,一直懒到现在才真正算是懂了一点点皮毛,就在这里记录下来...

  • 深度定制UITextFiled

    现在手机研发产品,产品经理会对产品的一些效果要求越来越严格,越来越精细,比如最近在遇到了在登录注册界面,对输入框的...

  • zxing二维码生车深度定制

    二维码生成服务之深度定制 之前写了一篇二维码服务定制的博文,现在则在之前的基础上,再进一步,花样的实现深度定制的需...

  • 定制酒现状及常规打法总结——日学一课2019-08-08

    近些年,由于酒业深度调整、白酒消费的深化,定制酒作为团购产品中的一个分支逐渐壮大!尤其是企业定制这个板块,随着定制...

  • Android ROM包定制(解包,增删模块,打包)

    以前刚用手机的时候,经常可以在玩机论坛上看到很多发ROM包的帖子,譬如什么大深度定制ROM,什么大深度深度精简纯净...

  • Deepin安装数据可视化工具redash开发版

    Deepin基于debian系统进行了深度的UI定制,相较于其他的国产定制版系统, 可以算得上是良心之作。最近因为...

  • 【个性定制酒】

    个性定制酒的5大特点 1.深度宣传,提升形象; 2.量身定制,彰显实力; 3.低调奢华,尊贵不贵; 4.专属服务,...

  • 个人品牌课程复盘及思考

    #课程复盘# 【第二周】开始创业:深度挖掘用户痛点并定制服务 关键词:用户痛点、定制服务 一、个人定位:找到专属于...

  • 福特野马GT深度定制

    福特野马GT深度定制? 打孔皮革&意大利Alcantara 座椅蜂巢绗缝+野性纹绣 内饰全皮革奢华包覆 碳纤维运动...

  • 深度揭秘 | 何为豪宅定制?

    “豪宅定制”一词,近年来频频出现在人们的视线中。自从定制概念于1970年在美国被提出后,全球大师们对顶级豪宅设计和...

网友评论

      本文标题:CollectionFlowLayout深度定制

      本文链接:https://www.haomeiwen.com/subject/fsybpttx.html