PSTGridLayoutInfo.m 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. //
  2. // PSTGridLayoutInfo.m
  3. // PSPDFKit
  4. //
  5. // Copyright (c) 2012-2013 Peter Steinberger. All rights reserved.
  6. //
  7. #import "PSTGridLayoutInfo.h"
  8. #import "PSTGridLayoutSection.h"
  9. #import "PSTGridLayoutItem.h"
  10. @interface PSTGridLayoutInfo () {
  11. NSMutableArray *_sections;
  12. CGRect _visibleBounds;
  13. CGSize _layoutSize;
  14. BOOL _isValid;
  15. }
  16. @property (nonatomic, strong) NSMutableArray *sections;
  17. @end
  18. @implementation PSTGridLayoutInfo
  19. ///////////////////////////////////////////////////////////////////////////////////////////
  20. #pragma mark - NSObject
  21. - (id)init {
  22. if ((self = [super init])) {
  23. _sections = [NSMutableArray new];
  24. }
  25. return self;
  26. }
  27. - (NSString *)description {
  28. return [NSString stringWithFormat:@"<%@: %p dimension:%.1f horizontal:%d contentSize:%@ sections:%@>", NSStringFromClass(self.class), self, self.dimension, self.horizontal, NSStringFromCGSize(self.contentSize), self.sections];
  29. }
  30. ///////////////////////////////////////////////////////////////////////////////////////////
  31. #pragma mark - Public
  32. - (PSTGridLayoutInfo *)snapshot {
  33. PSTGridLayoutInfo *layoutInfo = [self.class new];
  34. layoutInfo.sections = self.sections;
  35. layoutInfo.rowAlignmentOptions = self.rowAlignmentOptions;
  36. layoutInfo.usesFloatingHeaderFooter = self.usesFloatingHeaderFooter;
  37. layoutInfo.dimension = self.dimension;
  38. layoutInfo.horizontal = self.horizontal;
  39. layoutInfo.leftToRight = self.leftToRight;
  40. layoutInfo.contentSize = self.contentSize;
  41. return layoutInfo;
  42. }
  43. - (CGRect)frameForItemAtIndexPath:(NSIndexPath *)indexPath {
  44. PSTGridLayoutSection *section = self.sections[(NSUInteger)indexPath.section];
  45. CGRect itemFrame;
  46. if (section.fixedItemSize) {
  47. itemFrame = (CGRect){.size=section.itemSize};
  48. }else {
  49. itemFrame = [section.items[(NSUInteger)indexPath.item] itemFrame];
  50. }
  51. return itemFrame;
  52. }
  53. - (id)addSection {
  54. PSTGridLayoutSection *section = [PSTGridLayoutSection new];
  55. section.rowAlignmentOptions = self.rowAlignmentOptions;
  56. section.layoutInfo = self;
  57. [_sections addObject:section];
  58. [self invalidate:NO];
  59. return section;
  60. }
  61. - (void)invalidate:(BOOL)arg {
  62. _isValid = NO;
  63. }
  64. @end