Model cell
#import <UIKit/UIKit.h>@interface ImageCell : UICollectionViewCell@property (nonatomicNSString *image;@end
#import "ImageCell.h" @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
Controller 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
Heap Layout Stacklayout
#define RANDOM0_1 (Arc4random_uniform (+)/100.0)#import "StackLayout.h" @implementation stacklayout - (BOOL) Shouldinvalidatelayoutforboundschange: (CGRect) newbounds{return YES;}//-(cgsize) collectionviewcontentsize//{//Return Cgsizemake ($);//}-(Uicollectionviewlayoutattributes *) Layoutattributesforitematindexpath: (Nsindexpath*) indexpath{Nsarray*angles = @[@0, @(-0.2), @(-0.5), @(0.2), @(0.5)]; Uicollectionviewlayoutattributes *attrs = [Uicollectionviewlayoutattributes layoutattributesforcellwithindexpath: Indexpath]; Attrs. Size= Cgsizemake ( -, -); Attrs. Center= Cgpointmake ( Self. CollectionView. Frame. Size. Width*0.5, Self. CollectionView. Frame. Size. Height*0.5);if(Indexpath. Item>=5) {Attrs. Hidden=YES; }Else{Attrs. Transform= Cgaffinetransformmakerotation ([Angles[indexpath. Item] Floatvalue]);//The bigger the zindex, the more it is on topAttrs. ZIndex= [ Self. CollectionViewNumberofitemsinsection:indexpath. section]-Indexpath. Item; }returnAttrs;} - (Nsarray*) Layoutattributesforelementsinrect: (CGRect) rect{Nsmutablearray*array = [NsmutablearrayArray];NsintegerCount = [ Self. CollectionViewNumberofitemsinsection:0]; for(inti =0; i<count; i++) {Uicollectionviewlayoutattributes *attrs = [ Selflayoutattributesforitematindexpath:[NsindexpathINDEXPATHFORITEM:I insection:0]]; [Array addobject:attrs]; }returnArray;}@end
Circular layout Circlelayout
#import "CircleLayout.h" @implementation circlelayout - (BOOL) Shouldinvalidatelayoutforboundschange: (CGRect) newbounds{return YES;} -(Uicollectionviewlayoutattributes *) Layoutattributesforitematindexpath: (Nsindexpath*) indexpath{uicollectionviewlayoutattributes *attrs = [uicollectionviewlayoutattributes Layoutattributesforcellwithindexpath:indexpath]; Attrs. Size= Cgsizemake ( -, -);//radius of the circle CGFloatCircleradius = -;CgpointCirclecenter = Cgpointmake ( Self. CollectionView. Frame. Size. Width*0.5, Self. CollectionView. Frame. Size. Height*0.5);//The angle between each item CGFloatAngledelta = M_pi *2/ [ Self. CollectionViewNumberofitemsinsection:indexpath. section];//Calculate the angle of the current item CGFloatAngle = Indexpath. Item* ANGLEDELTA; Attrs. Center= Cgpointmake (circlecenter. x+ Circleradius * COSF (angle), circlecenter. Y-Circleradius * Sinf (angle)); Attrs. ZIndex= Indexpath. Item;returnAttrs;} - (Nsarray*) Layoutattributesforelementsinrect: (CGRect) rect{Nsmutablearray*array = [NsmutablearrayArray];NsintegerCount = [ Self. CollectionViewNumberofitemsinsection:0]; for(inti =0; i<count; i++) {Uicollectionviewlayoutattributes *attrs = [ Selflayoutattributesforitematindexpath:[NsindexpathINDEXPATHFORITEM:I insection:0]]; [Array addobject:attrs]; }returnArray;}@end
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
iOS development-Uicollectionviewlayout custom layouts