YBIBCollectionViewLayout.m 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. //
  2. // YBIBCollectionViewLayout.m
  3. // YBImageBrowserDemo
  4. //
  5. // Created by 杨少 on 2018/4/17.
  6. // Copyright © 2018年 波儿菜. All rights reserved.
  7. //
  8. #import "YBIBCollectionViewLayout.h"
  9. @implementation YBIBCollectionViewLayout
  10. - (instancetype)init {
  11. self = [super init];
  12. if (self) {
  13. self.minimumLineSpacing = 0;
  14. self.minimumInteritemSpacing = 0;
  15. self.sectionInset = UIEdgeInsetsZero;
  16. self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  17. _distanceBetweenPages = 20;
  18. }
  19. return self;
  20. }
  21. - (void)prepareLayout {
  22. [super prepareLayout];
  23. self.itemSize = self.collectionView.bounds.size;
  24. }
  25. - (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
  26. NSArray<UICollectionViewLayoutAttributes *> *layoutAttsArray = [[NSArray alloc] initWithArray:[super layoutAttributesForElementsInRect:rect] copyItems:YES];
  27. CGFloat halfWidth = self.collectionView.bounds.size.width / 2.0;
  28. CGFloat centerX = self.collectionView.contentOffset.x + halfWidth;
  29. [layoutAttsArray enumerateObjectsUsingBlock:^(UICollectionViewLayoutAttributes * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  30. obj.center = CGPointMake(obj.center.x + (obj.center.x - centerX) / halfWidth * self.distanceBetweenPages / 2, obj.center.y);
  31. }];
  32. return layoutAttsArray;
  33. }
  34. - (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
  35. return YES;
  36. }
  37. @end