Custom UICollectionView Layout-linear Layout
Linear Layout
The previous section describes the basic implementation of custom la S. This section mainly uses custom la s to achieve linear la S and make a simple image browser effect.
Custom Layout Class 1. custom Layout class
/// Inherited from UICollectionViewFlowLayout, because the flow layout has the left and right scrolling function @ interface CYLineLayout: UICollectionViewFlowLayout @ end
2. Re-parent class method 2.1 prepare layout
/// The preparation operation is generally set here with some initialization parameters-(void) prepareLayout {// you must call the parent class (the parent class also has some preparation operations) [super prepareLayout]; CGFloat width = self. collectionView. frame. size. height * 0.7; self. itemSize = CGSizeMake (width, width); self. scrollDirection = uicollectionviewscrolldirehorizontal; self. minimumLineSpacing = 20; // set the padding CGFloat inset = (self. collectionView. frame. size. width-width) * 0.5; self. sectionInset = UIEdgeInsetsMake (0, inset, 0, inset );}
2.2 return the layout attributes of all elements (such as cells) in collectionView.
///// Return the layout attributes of all elements (such as cells) in collectionView: This method determines how cells are arranged. // each cell has its own layout attributes: UICollectionViewLayoutAttributes // The returned array must contain the UICollectionViewLayoutAttributes object //-(NSArray *) attributes :( CGRect) rect {NSArray * array = [super attributes: rect]; // calculate the midpoint CGFloat centerX = self of CollectionView. collectionView. contentOffset. x + self. collectionView. frame. size. width * 0.5; for (UICollectionViewLayoutAttributes * attrs in array) {// calculate the difference between the x value of the cell midpoint and centerX CGFloat delta = ABS (centerX-attrs. center. x); CGFloat scale = 1-delta/self. collectionView. frame. size. width; attrs. transform = CGAffineTransformMakeScale (scale, scale);} return array ;}
2.3 whether to refresh the layout when the bounds of the uicollectionView changes
/// Whether to refresh the layout when the bounds of the uicollectionView changes-(BOOL) shouldInvalidateLayoutForBoundsChange :( CGRect) newBounds {return YES ;}
2.4 return the final offset of collectionView (the final position)
/// TargetContentOffset: After modification, collectionView's final contentOffset (take the decision) ///// proposedContentOffset: by default, collectionView final contentOffset /// @ param velocity rate //-(CGPoint) ratio :( CGPoint) proposedContentOffset withScrollingVelocity :( CGPoint) velocity {// NSLog (@ "% @", NSStringFromCGPoint (proposedContentOffset); CGSize size = self. collectionView. frame. size; // calculate the area of the visible area CGRect rect = CGRectMake (proposedContentOffset. x, proposedContentOffset. y, size. width, size. height); NSArray * array = [super layoutAttributesForElementsInRect: rect]; // calculates the value of CGFloat centerX = proposedContentOffset in CollectionView. x + self. collectionView. frame. size. width * 0.5; // indicates the shortest interval between the cell and the UICollectionView. CGFloat minDetal = MAXFLOAT; for (UICollectionViewLayoutAttributes * attrs in array) {if (ABS (minDetal)> ABS (centerX-attrs. center. x) {minDetal = attrs. center. x-centerX;} return CGPointMake (proposedContentOffset. x + minDetal, proposedContentOffset. y );}
3. The controller uses layout 3.1 to import the header file
#import "CYLineLayout.h"
3.2 instantiation Layout
CYLineLayout * layout = [[CYLineLayout alloc] init]; // linear layout UICollectionView * collectionView = [[UICollectionView alloc] initWithFrame: frame collectionViewLayout: layout];
3.3 Data Source Method of UICollectionView
// ……