Guest User

Untitled

a guest
Dec 11th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. // call the two layout calculating methods , and combine.
  2. - (void)prepareLayout{
  3. [super prepareLayout];
  4. if(self.attributesArray.count>0){
  5. [self.attributesArray removeAllObjects];
  6. }
  7. NSInteger indexCount = [self.collectionView numberOfItemsInSection: 0];
  8. for (NSInteger i = 0; i<indexCount; i++) {
  9. NSIndexPath * indexPath = [NSIndexPath indexPathForItem:i inSection: 0];
  10. UICollectionViewLayoutAttributes * attributes = [self layoutAttributesForItemAtIndexPath: indexPath];
  11. [self.attributesArray addObject: attributes];
  12. }
  13. UICollectionViewLayoutAttributes * headAttributes = [self layoutAttributesForSupplementaryViewOfKind: UICollectionElementKindSectionHeader atIndexPath: [NSIndexPath indexPathForItem:0 inSection: 0]];
  14. [self.attributesArray addObject: headAttributes];
  15. }
  16.  
  17. // calculate the items layout
  18. - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath{
  19. UICollectionViewLayoutAttributes * cellAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath: indexPath];
  20. CGFloat baseOriginY = 133;
  21. switch (indexPath.item) {
  22. case 0:
  23. {
  24. cellAttributes.frame = CGRectMake(0, baseOriginY, KScreenWidth, 50);
  25. }
  26. break;
  27. case 6: //7:
  28. case 7: //8:
  29. {
  30. NSInteger count = indexPath.item-6;
  31. cellAttributes.frame = CGRectMake(0, 155 + 50*count + baseOriginY, KScreenWidth, 50);
  32. }
  33. break;
  34. case 5: //6:
  35. {
  36. cellAttributes.frame = CGRectMake(0, 145 + baseOriginY, KScreenWidth, 10);
  37. }
  38. break;
  39. default:
  40. {
  41. NSInteger i = indexPath.item - 1;
  42. cellAttributes.frame = CGRectMake(i*KScreenWidth/4.0, 50 + baseOriginY, KScreenWidth/4.0, 95);
  43. }
  44. break;
  45. }
  46. return cellAttributes;
  47. }
  48.  
  49. // calculate the headers layout
  50. - (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath{
  51. UICollectionViewLayoutAttributes * headerAttributes = [UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind: UICollectionElementKindSectionHeader withIndexPath: indexPath];
  52. // headerAttributes.size = CGSizeMake(KScreenWidth, 133);
  53. headerAttributes.frame = CGRectMake(0, 0, KScreenWidth, 133);
  54. return headerAttributes;
  55.  
  56. }
  57.  
  58. // return the final layout results
  59. - (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect{
  60. return self.attributesArray;
  61. }
Add Comment
Please, Sign In to add comment