PSTCollectionViewLayout.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //
  2. // PSTCollectionViewLayout.h
  3. // PSPDFKit
  4. //
  5. // Copyright (c) 2012-2013 Peter Steinberger. All rights reserved.
  6. //
  7. #import "PSTCollectionViewCommon.h"
  8. #import <CoreGraphics/CoreGraphics.h>
  9. #import <QuartzCore/QuartzCore.h>
  10. typedef NS_ENUM(NSUInteger, PSTCollectionViewItemType) {
  11. PSTCollectionViewItemTypeCell,
  12. PSTCollectionViewItemTypeSupplementaryView,
  13. PSTCollectionViewItemTypeDecorationView
  14. };
  15. // The PSTCollectionViewLayout class is provided as an abstract class for subclassing to define custom collection layouts.
  16. // Defining a custom layout is an advanced operation intended for applications with complex needs.
  17. @class PSTCollectionViewLayoutAttributes, PSTCollectionView;
  18. @interface PSTCollectionViewLayoutAttributes : NSObject <NSCopying>
  19. @property (nonatomic) CGRect frame;
  20. @property (nonatomic) CGPoint center;
  21. @property (nonatomic) CGSize size;
  22. @property (nonatomic) CATransform3D transform3D;
  23. @property (nonatomic) CGFloat alpha;
  24. @property (nonatomic) NSInteger zIndex; // default is 0
  25. @property (nonatomic, getter=isHidden) BOOL hidden; // As an optimization, PSTCollectionView might not create a view for items whose hidden attribute is YES
  26. @property (nonatomic, strong) NSIndexPath *indexPath;
  27. + (instancetype)layoutAttributesForCellWithIndexPath:(NSIndexPath *)indexPath;
  28. + (instancetype)layoutAttributesForSupplementaryViewOfKind:(NSString *)elementKind withIndexPath:(NSIndexPath *)indexPath;
  29. + (instancetype)layoutAttributesForDecorationViewOfKind:(NSString *)kind withIndexPath:(NSIndexPath *)indexPath;
  30. /*
  31. + (id)layoutAttributesForDecorationViewOfKind:(id)arg1 withIndexPath:(id)arg2;
  32. - (id)initialLayoutAttributesForInsertedDecorationElementOfKind:(id)arg1 atIndexPath:(id)arg2;
  33. - (BOOL)_isEquivalentTo:(id)arg1;
  34. */
  35. @end
  36. @interface PSTCollectionViewLayoutAttributes (Private)
  37. @property (nonatomic, readonly) NSString *representedElementKind;
  38. @property (nonatomic, readonly) PSTCollectionViewItemType representedElementCategory;
  39. - (BOOL)isDecorationView;
  40. - (BOOL)isSupplementaryView;
  41. - (BOOL)isCell;
  42. @end
  43. @interface PSTCollectionViewLayout : NSObject <NSCoding>
  44. // Methods in this class are meant to be overridden and will be called by its collection view to gather layout information.
  45. // To get the truth on the current state of the collection view, call methods on PSTCollectionView rather than these.
  46. @property (nonatomic, unsafe_unretained, readonly) PSTCollectionView *collectionView;
  47. // Call -invalidateLayout to indicate that the collection view needs to requery the layout information.
  48. // Subclasses must always call super if they override.
  49. - (void)invalidateLayout;
  50. /// @name Registering Decoration Views
  51. - (void)registerClass:(Class)viewClass forDecorationViewOfKind:(NSString *)kind;
  52. - (void)registerNib:(UINib *)nib forDecorationViewOfKind:(NSString *)kind;
  53. @end
  54. @interface PSTCollectionViewLayout (SubclassingHooks)
  55. + (Class)layoutAttributesClass; // override this method to provide a custom class to be used when instantiating instances of PSTCollectionViewLayoutAttributes
  56. // The collection view calls -prepareLayout once at its first layout as the first message to the layout instance.
  57. // The collection view calls -prepareLayout again after layout is invalidated and before requerying the layout information.
  58. // Subclasses should always call super if they override.
  59. - (void)prepareLayout;
  60. // PSTCollectionView calls these four methods to determine the layout information.
  61. // Implement -layoutAttributesForElementsInRect: to return layout attributes for supplementary or decoration views, or to perform layout in an as-needed-on-screen fashion.
  62. // Additionally, all layout subclasses should implement -layoutAttributesForItemAtIndexPath: to return layout attributes instances on demand for specific index paths.
  63. // If the layout supports any supplementary or decoration view types, it should also implement the respective atIndexPath: methods for those types.
  64. - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect; // return an array layout attributes instances for all the views in the given rect
  65. - (PSTCollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath;
  66. - (PSTCollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath;
  67. - (PSTCollectionViewLayoutAttributes *)layoutAttributesForDecorationViewOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath;
  68. - (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds; // return YES to cause the collection view to requery the layout for geometry information
  69. - (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity; // return a point at which to rest after scrolling - for layouts that want snap-to-point scrolling behavior
  70. - (CGSize)collectionViewContentSize; // the collection view calls this to update its content size any time it queries new layout information - at least one of the width and height fields must match the respective field of the collection view's bounds
  71. @end
  72. @interface PSTCollectionViewLayout (UpdateSupportHooks)
  73. // This method is called when there is an update with deletes/inserts to the collection view.
  74. // It will be called prior to calling the initial/final layout attribute methods below to give the layout an opportunity to do batch computations for the insertion and deletion layout attributes.
  75. // The updateItems parameter is an array of PSTCollectionViewUpdateItem instances for each element that is moving to a new index path.
  76. - (void)prepareForCollectionViewUpdates:(NSArray *)updateItems;
  77. // This method is called inside an animation block after all items have been laid out for a collection view update.
  78. // Subclasses can use this opportunity to layout their 'layout-owned' decoration views in response to the update.
  79. - (void)finalizeCollectionViewUpdates;
  80. // Collection view calls these methods to determine the starting layout for animating in newly inserted views, or the ending layout for animating out deleted views
  81. - (PSTCollectionViewLayoutAttributes *)initialLayoutAttributesForAppearingItemAtIndexPath:(NSIndexPath *)itemIndexPath;
  82. - (PSTCollectionViewLayoutAttributes *)finalLayoutAttributesForDisappearingItemAtIndexPath:(NSIndexPath *)itemIndexPath;
  83. - (PSTCollectionViewLayoutAttributes *)initialLayoutAttributesForInsertedSupplementaryElementOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)elementIndexPath;
  84. - (PSTCollectionViewLayoutAttributes *)finalLayoutAttributesForDeletedSupplementaryElementOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)elementIndexPath;
  85. @end
  86. @interface PSTCollectionViewLayout (Private)
  87. - (void)setCollectionViewBoundsSize:(CGSize)size;
  88. - (PSTCollectionReusableView *)decorationViewForCollectionView:(PSTCollectionView *)collectionView withReuseIdentifier:(NSString *)reuseIdentifier indexPath:(NSIndexPath *)indexPath;
  89. @end