Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // call the two layout calculating methods , and combine.
- - (void)prepareLayout{
- [super prepareLayout];
- if(self.attributesArray.count>0){
- [self.attributesArray removeAllObjects];
- }
- NSInteger indexCount = [self.collectionView numberOfItemsInSection: 0];
- for (NSInteger i = 0; i<indexCount; i++) {
- NSIndexPath * indexPath = [NSIndexPath indexPathForItem:i inSection: 0];
- UICollectionViewLayoutAttributes * attributes = [self layoutAttributesForItemAtIndexPath: indexPath];
- [self.attributesArray addObject: attributes];
- }
- UICollectionViewLayoutAttributes * headAttributes = [self layoutAttributesForSupplementaryViewOfKind: UICollectionElementKindSectionHeader atIndexPath: [NSIndexPath indexPathForItem:0 inSection: 0]];
- [self.attributesArray addObject: headAttributes];
- }
- // calculate the items layout
- - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath{
- UICollectionViewLayoutAttributes * cellAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath: indexPath];
- CGFloat baseOriginY = 133;
- switch (indexPath.item) {
- case 0:
- {
- cellAttributes.frame = CGRectMake(0, baseOriginY, KScreenWidth, 50);
- }
- break;
- case 6: //7:
- case 7: //8:
- {
- NSInteger count = indexPath.item-6;
- cellAttributes.frame = CGRectMake(0, 155 + 50*count + baseOriginY, KScreenWidth, 50);
- }
- break;
- case 5: //6:
- {
- cellAttributes.frame = CGRectMake(0, 145 + baseOriginY, KScreenWidth, 10);
- }
- break;
- default:
- {
- NSInteger i = indexPath.item - 1;
- cellAttributes.frame = CGRectMake(i*KScreenWidth/4.0, 50 + baseOriginY, KScreenWidth/4.0, 95);
- }
- break;
- }
- return cellAttributes;
- }
- // calculate the headers layout
- - (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath{
- UICollectionViewLayoutAttributes * headerAttributes = [UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind: UICollectionElementKindSectionHeader withIndexPath: indexPath];
- // headerAttributes.size = CGSizeMake(KScreenWidth, 133);
- headerAttributes.frame = CGRectMake(0, 0, KScreenWidth, 133);
- return headerAttributes;
- }
- // return the final layout results
- - (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect{
- return self.attributesArray;
- }
Add Comment
Please, Sign In to add comment