// // DDCollectionViewHorizontalLayout.m // TestCollectionView // // Created by 刘卫林 on 15/08/27. #import"LWLCollectionViewHorizontalLayout.h" @implementation LWLCollectionViewHorizontalLayout - (void)prepareLayout { [super prepareLayout]; //self.rowCount = 2; //self.itemCountPerRow = 5; self.allAttributes = [NSMutableArray array]; NSUInteger count = [self.collectionView numberOfItemsInSection:0]; for (NSUInteger i = 0; i *)layoutAttributesForElementsInRect:(CGRect)rect { NSArray *attributes = [super layoutAttributesForElementsInRect:rect]; NSMutableArray *tmp = [NSMutableArray array]; for (UICollectionViewLayoutAttributes *attr in attributes) { for (UICollectionViewLayoutAttributes *attr2 in self.allAttributes) { if (attr.indexPath.item == attr2.indexPath.item) { [tmp addObject:attr2]; break; } } } return tmp; } // 根据 item 计算目标item的位置 // x 横向偏移 y 竖向偏移 - (void)targetPositionWithItem:(NSUInteger)item resultX:(NSUInteger *)x resultY:(NSUInteger *)y { NSUInteger page = item/(self.itemCountPerRow*self.rowCount); NSUInteger theX = item % self.itemCountPerRow + page * self.itemCountPerRow; NSUInteger theY = item / self.itemCountPerRow - page * self.rowCount; if (x != NULL) { *x = theX; } if (y != NULL) { *y = theY; } } // 根据偏移量计算item - (NSUInteger)originItemAtX:(NSUInteger)x y:(NSUInteger)y { NSUInteger item = x * self.rowCount + y; return item; } @end