// // YBIBCollectionViewLayout.m // YBImageBrowserDemo // // Created by 杨少 on 2018/4/17. // Copyright © 2018年 波儿菜. All rights reserved. // #import "YBIBCollectionViewLayout.h" @implementation YBIBCollectionViewLayout - (instancetype)init { self = [super init]; if (self) { self.minimumLineSpacing = 0; self.minimumInteritemSpacing = 0; self.sectionInset = UIEdgeInsetsZero; self.scrollDirection = UICollectionViewScrollDirectionHorizontal; _distanceBetweenPages = 20; } return self; } - (void)prepareLayout { [super prepareLayout]; self.itemSize = self.collectionView.bounds.size; } - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect { NSArray *layoutAttsArray = [[NSArray alloc] initWithArray:[super layoutAttributesForElementsInRect:rect] copyItems:YES]; CGFloat halfWidth = self.collectionView.bounds.size.width / 2.0; CGFloat centerX = self.collectionView.contentOffset.x + halfWidth; [layoutAttsArray enumerateObjectsUsingBlock:^(UICollectionViewLayoutAttributes * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { obj.center = CGPointMake(obj.center.x + (obj.center.x - centerX) / halfWidth * self.distanceBetweenPages / 2, obj.center.y); }]; return layoutAttsArray; } - (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds { return YES; } @end