The introduction of UICollectionViewLayoutAttributes class # # #

@property (nonatomic) CGRect frame
@property (nonatomic) CGPoint center
@property (nonatomic) CGSize size
@property (nonatomic) CATransform3D transform3D
@property (nonatomic) CGFloat alpha
@property (nonatomic) NSInteger zIndex
@property (nonatomic, getter=isHidden) BOOL hidden

Copy the code

As you can see, UICollectionViewLayoutAttributes instance includes such as borders, center, size, shape, transparency, hierarchical relationships, and whether the hidden information. 1. 2. A cell corresponding to a UICollectionViewLayoutAttributes object UICollectionViewLayoutAttributes object determines the decoration of the cell location (frame)

Custom UICollectionViewLayout

  • UICollectionViewLayout provides layout information to UICollectionView, including not only cell layout information, but also append view and decorate view layout information. A common way to implement a custom Layout is to inherit the UICollectionViewLayout class and override the following methods:
-(void)prepareLayout The prepareLayout method is automatically called to ensure that the Layout instance is correct. - (CGSize) collectionViewContentSize return collectionView content size - (NSArray *) layoutAttributesForElementsInRect (CGRect) the rect 1. Returns all the layout attribute of the element (2) of the rect returns include UICollectionViewLayoutAttributes NSArray 3. UICollectionViewLayoutAttributes can be a cell, Additional views or decoration view information, through different UICollectionViewLayoutAttributes initialization method can get different types of UICollectionViewLayoutAttributes:  1)layoutAttributesForCellWithIndexPath: 2)layoutAttributesForSupplementaryViewOfKind:withIndexPath: 3)layoutAttributesForDecorationViewOfKind:withIndexPath: -(UICollectionViewLayoutAttributes )layoutAttributesForItemAtIndexPath:(NSIndexPath )indexPath Returns the position of the corresponding to the indexPath cell layout properties - UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind: (nsstrings )kind atIndexPath:(NSIndexPath *)indexPath returns the layout property of the append view corresponding to the position of the indexPath, If there is no additional views don't overload - layoutAttributesForDecorationViewOfKind: (nsstrings) decorationViewKind UICollectionViewLayoutAttributes (*) AtIndexPath :(NSIndexPath)indexPath returns the layout properties of the decorator view corresponding to the position of the indexPath, If there is no decoration view don't overload - (BOOL) shouldInvalidateLayoutForBoundsChange: (CGRect) newBounds when boundary changes, whether should refresh layout. If YES, the required layout information will be recalculated when the boundary changes (typically scroll to somewhere else).Copy the code
  • Call to order
1) -(void)prepareLayout Sets the layout structure and initial parameters. 2) - (CGSize) collectionViewContentSize determine collectionView all the size of the content. 3) - (NSArray *) layoutAttributesForElementsInRect: (CGRect) the rect the appearance of the initial layout will be decided by the method returns UICollectionViewLayoutAttributes. 1) -invalidatelayOut is sent to the current layout when the layout needs to be updated. This message will be returned immediately and the next loop is scheduled to refresh the current layout. 2)-prepareLayout, 3) in order to call - collectionViewContentSize and - layoutAttributesForElementsInRect to generate an updated layout.Copy the code