FUColourFlowLayout.m 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //
  2. // FUColourFlowLayout.m
  3. // FULiveDemo
  4. //
  5. // Created by 孙慕 on 2019/5/31.
  6. // Copyright © 2019 FaceUnity. All rights reserved.
  7. //
  8. #import "FUColourFlowLayout.h"
  9. @implementation FUColourFlowLayout
  10. //在这个方法中,UICollectionView还没有实例化,所以取不到其对应的frame
  11. -(instancetype)init{
  12. if (self == [super init]) {
  13. }
  14. return self;
  15. }
  16. //当布局刷新的时候会自动调用这个方法
  17. -(void)prepareLayout{
  18. [super prepareLayout];
  19. //修改滚动方向 为水平方向来滚动
  20. self.scrollDirection = UICollectionViewScrollDirectionVertical;
  21. //获取对应UICollectionView的Size
  22. CGSize collectionSize = self.collectionView.frame.size;
  23. //定义显示ITEM的 宽 高
  24. CGFloat itemWidth = collectionSize.width*0.6;
  25. CGFloat itemHeight = collectionSize.width*0.6;
  26. //修改ITEM大小
  27. self.itemSize = CGSizeMake(itemWidth, itemHeight);
  28. //设置头部和尾部的初始间距
  29. // CGFloat topMargin = collectionSize.height/2-itemWidth/2;
  30. // self.sectionInset = UIEdgeInsetsMake(topMargin,0, topMargin,0 );
  31. }
  32. //返回所的有的Item对应的属性设置
  33. -(NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect{
  34. //取出所有的Item对应的属性
  35. NSArray *superAttributesArray = [super layoutAttributesForElementsInRect:rect];
  36. //计算中心点
  37. CGFloat screenCenter = self.collectionView.contentOffset.y+self.collectionView.frame.size.height/2;
  38. //循环设置Item 的属性
  39. for (UICollectionViewLayoutAttributes *attributes in superAttributesArray) {
  40. //计算 差值
  41. CGFloat deltaMargin = ABS(screenCenter - attributes.center.y);
  42. //计算放大比例
  43. CGFloat scale = 1 - deltaMargin/(self.collectionView.frame.size.height/2+attributes.size.height) * 0.9;
  44. //设置
  45. attributes.transform = CGAffineTransformMakeScale(scale, scale);
  46. }
  47. return superAttributesArray;
  48. }
  49. //当手指离开屏幕时会调用此方法
  50. -(CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity{
  51. //取出屏幕的中心点
  52. CGFloat screenCenter = proposedContentOffset.y +self.collectionView.frame.size.height/2;
  53. //取出可见范围内的Cell
  54. CGRect visibleRect = CGRectZero;
  55. visibleRect.size = self.collectionView.frame.size;
  56. visibleRect.origin = proposedContentOffset;
  57. NSArray *visibleArray = [super layoutAttributesForElementsInRect:visibleRect];
  58. CGFloat minMargin = MAXFLOAT;
  59. for (UICollectionViewLayoutAttributes *attributes in visibleArray) {
  60. CGFloat deltaMargin = attributes.center.y - screenCenter;
  61. if (ABS(minMargin)>ABS(deltaMargin)) {
  62. minMargin = deltaMargin;
  63. }
  64. }
  65. return CGPointMake(proposedContentOffset.x, proposedContentOffset.y + minMargin);
  66. }
  67. //当屏幕的可见范围发生变化 的时候
  68. //重新刷新视图
  69. -(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds{
  70. return YES;
  71. }
  72. @end