Model cell
@interface ImageCell : UICollectionViewCell@property (nonatomicNSString *image;@end
@interface imagecell()@property(Weak,nonatomic)Iboutlet Uiimageview*imageview;@end @implementation Imagecell - (void) awakefromnib { Self. ImageView. Layer. BorderWidth=3; Self. ImageView. Layer. BorderColor= [UicolorWhitecolor]. Cgcolor; Self. ImageView. Layer. Cornerradius=3; Self. ImageView. Clipstobounds=YES;} - (void) SetImage: (NSString*) image{_image = [image copy]; Self. ImageView. Image= [UIImageImagenamed:image];}@end
Linelayout
#Import "LineLayout.h"Static ConstCGFloat ITEMWH = -;@implementationlinelayout-(instancetype) init{if(self = [SuperInit]) {}returnSelf;}/** * Re-layout as long as the displayed boundary changes: Internally the Preparelayout and Layoutattributesforelementsinrect methods are called back to get the layout properties of all cells */-(BOOL) Shouldinvalidatelayoutforboundschange: (CGRect) newbounds{returnYES;}/** * Used to set CollectionView stop scrolling at that moment of position * * @param proposedcontentoffset originally CollectionView stop scrolling at that moment of position * @para M Velocity Scrolling speed * /-(Cgpoint) Targetcontentoffsetforproposedcontentoffset: (cgpoint) Proposedcontentoffset withScrollingVelocity: ( Cgpoint) velocity{//1. Calculate the range of ScrollView last stopCGRect Lastrect; Lastrect.origin = Proposedcontentoffset; Lastrect.size = self.collectionView.frame.size;//Calculate the middle of the screen xCGFloat CenterX = proposedcontentoffset.x + self.collectionView.frame.size.width *0.5;//2. Remove all attributes within this rangeNsarray *array = [self layoutattributesforelementsinrect:lastrect];//3. Traverse All PropertiesCGFloat adjustoffsetx = maxfloat; for(uicollectionviewlayoutattributes *attrs in array) {if(ABS (Attrs.center.x-centerx) < ABS (ADJUSTOFFSETX)) {adjustoffsetx = Attrs.center.x-centerx; } }returnCgpointmake (Proposedcontentoffset.x + adjustoffsetx, proposedcontentoffset.y);}/** * Some of the initialization work is best implemented here */- (void) preparelayout{[SuperPreparelayout];//size of each cellSelf.itemsize = Cgsizemake (Itemwh, ITEMWH); CGFloat inset = (SELF.COLLECTIONVIEW.FRAME.SIZE.WIDTH-ITEMWH) *0.5; Self.sectioninset = Uiedgeinsetsmake (0, inset,0, inset);//Set Horizontal scrollingSelf.scrolldirection = Uicollectionviewscrolldirectionhorizontal; self.minimumlinespacing = Itemwh *0.7;//Each cell (item) has its own uicollectionviewlayoutattributes //Every indexpath has its own uicollectionviewlayoutattributes}/** Effective Distance: When the middle X of item is within the middle X of the screen, it will start to zoom in, and the other case is reduced .StaticCGFloatConstActivedistance = Max;/** Scaling factor: The larger the value, the greater the item will be */StaticCGFloatConstScalefactor =0.6;-(Nsarray *) Layoutattributesforelementsinrect: (CGRect) rect{//0. Calculate the visible rectangular boxCGRect Visiablerect; Visiablerect.size = self.collectionView.frame.size; Visiablerect.origin = Self.collectionView.contentOffset;//1. Get the default cell uicollectionviewlayoutattributesNsarray *array = [SuperLayoutattributesforelementsinrect:rect];//Calculate the middle of the screen xCGFloat CenterX = self.collectionview.contentoffset.x + self.collectionView.frame.size.width *0.5;//2. Traverse All layout Properties for(uicollectionviewlayoutattributes *attrs in array) {//If not on the screen, skip directly if(! Cgrectintersectsrect (Visiablerect, Attrs.frame))Continue;//midpoint of each item xCGFloat Itemcenterx = attrs.center.x;///The smaller the gap, the greater the scale //Calculate the scale based on the distance from the screen to the centerCGFloat scale =1+ Scalefactor * (1-(ABS (Itemcenterx-centerx)/activedistance)); Attrs.transform = Cgaffinetransformmakescale (scale, scale); }returnArray;}@end
Viewcontroller
#import "ViewController.h" #import "ImageCell.h" #import "LineLayout.h" @interface viewcontroller () <Uicollectionviewdatasource, Uicollectionviewdelegate>@property(nonatomic,Strong)Nsmutablearray*images;@property(nonatomic,Weak) Uicollectionview *collectionview;@end @implementation viewcontroller Static NSString*ConstID = @"Image";- (Nsmutablearray*) images{if(!_images) { Self. Images= [[NsmutablearrayALLOC] init]; for(inti =1; i<= -; i++) {[ Self. Imagesaddobject:[NSStringstringwithformat:@"%d", I]]; } }return_images;} - (void) Viewdidload {[SuperViewdidload];CGFloatW = Self. View. Frame. Size. Height;CGRectRect = CGRectMake (0,0W $); Uicollectionview *collectionview = [[Uicollectionview alloc] Initwithframe:rect collectionviewlayout:[[linelayout ALLOC] [init]]; CollectionView. DataSource= Self; CollectionView. Delegate= Self; [CollectionView registernib:[uinib nibwithnibname:@"Imagecell"BundleNil] Forcellwithreuseidentifier:id]; [ Self. ViewAddsubview:collectionview]; Self. CollectionView= CollectionView;//Collectionviewlayout: //Uicollectionviewlayout //Uicollectionviewflowlayout}- (void) Touchesbegan: (Nsset *) touches withevent: (uievent *) event{if([ Self. CollectionView. CollectionviewlayoutIskindofclass:[linelayout class]]) {[ Self. CollectionViewSetcollectionviewlayout:[[uicollectionviewflowlayout Alloc] init] Animated:YES]; }Else{ [ Self. CollectionViewSetcollectionviewlayout:[[linelayout Alloc] init] Animated:YES]; }}#pragma mark-<UICollectionViewDataSource>- (Nsinteger) CollectionView: (Uicollectionview *) CollectionView numberofitemsinsection: (Nsinteger) section{return Self. Images. Count;} -(Uicollectionviewcell *) CollectionView: (Uicollectionview *) CollectionView Cellforitematindexpath: (Nsindexpath*) indexpath{Imagecell *cell = [CollectionView dequeuereusablecellwithreuseidentifier:id ForIndexPath:indexPath]; Cell. Image= Self. Images[Indexpath. Item];returnCell;} - (void) CollectionView: (Uicollectionview *) CollectionView Didselectitematindexpath: (Nsindexpath*) indexpath{//delete model data[ Self. ImagesRemoveobjectatindex:indexpath. Item];//Delete ui (Refresh UI)[CollectionView Deleteitemsatindexpaths:@[indexpath];}@end
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
iOS Development-Uicollectionviewflowlayout pipelining layout