Before the waterfall used to use the Uicollectionview, but at that time using the system from the Uicollectionviewflowlayout layout, read the article today, see Uicollectionviewflowlayout Custom Related things, so hands-on wrote a simple picture browsing demo, skilled some uicollectionviewflowlayout custom layout.
#import <UIKit/UIKit.h>
@interface jwcollectionviewflowlayout:uicollectionviewflowlayout
@end
Custom Uicollectionviewflowlayout, first inherit uicollectionviewflowlayout, implement a few methods
#define SCREENWIDTH [UIScreen mainscreen].bounds.size.width #define Maxchangerange #import "Jwcollectionviewflowla Yout.h "@implementation jwcollectionviewflowlayout-(void) Preparelayout {self.scrolldirection =
Uicollectionviewscrolldirectionhorizontal;
Self.itemsize = Cgsizemake (300, 500);
}-(BOOL) Shouldinvalidatelayoutforboundschange: (CGRect) newbounds {return YES;} -(Nullable nsarray<__kindof uicollectionviewlayoutattributes *> *) Layoutattributesforelementsinrect: (CGRect)
Rect {Nsarray *array = [Super Layoutattributesforelementsinrect:rect]; CGRect visiblerect = CGRectMake (self.collectionview.contentoffset.x, 0, Self.collectionView.bounds.size.width,
Self.collectionView.bounds.size.height); For (uicollectionviewlayoutattributes *attr in array) {if (Cgrectintersectsrect (Attr.frame, rect)) {BOOL Isatrigh
t = YES;
CGFloat distance = (Attr.center.x-cgrectgetmidx (visiblerect));
if (distance<0) {distance =-distance;
Isatright = NO; } cgfloat precent;
if (distance < 180) {precent = 1.0;
else {precent = ((SCREENWIDTH/2)-distance)/(SCREENWIDTH/2);
} Catransform3d transform = catransform3didentity;
TRANSFORM.M34 = 1.0/600;
if (Precent < 0.5) {precent = 0.5;
} transform = Catransform3dscale (transform, 1, precent, 1); CGFloat p = isatright?
M_pi_4:-m_pi_4;
Transform = Catransform3drotate (transform, p * (1-precent), 0, 1, 0);
Attr.transform3d = transform;
Attr.zindex = 1;
Attr.alpha = precent;
} return array; }-(Cgpoint) Targetcontentoffsetforproposedcontentoffset: (cgpoint) Proposedcontentoffset withScrollingVelocity: (
Cgpoint) Velocity {cgfloat offset = maxfloat;
CGFloat hcenter = Proposedcontentoffset.x + (cgrectgetwidth (self.collectionView.bounds)/2.0); CGRect currentrect = CGRectMake (proposedcontentoffset.x, 0, Self.collectionView.bounds.size.width,
Self.collectionView.bounds.size.height); nsarray* array = [supER Layoutattributesforelementsinrect:currentrect]; For (uicollectionviewlayoutattributes* layoutattributes in array) {CGFloat Itemhorizontalcenter = LayoutAttributes.cen
ter.x;
if (ABS (Itemhorizontalcenter-hcenter) < ABS (offset) {offset = Itemhorizontalcenter-hcenter;
} return Cgpointmake (Proposedcontentoffset.x + offset, proposedcontentoffset.y);
}
Use
-(void) setupui
{
jwcollectionviewflowlayout *flowlayout = [[Jwcollectionviewflowlayout alloc] init];
Uicollectionview *imgbrowseview = [[Uicollectionview alloc] initWithFrame:self.view.bounds collectionviewlayout: FlowLayout];
Imgbrowseview.datasource = self;
Imgbrowseview.delegate = self;
Imgbrowseview.backgroundcolor = [Uicolor whitecolor];
[Self.view Addsubview:imgbrowseview];
_imgbrowseview = Imgbrowseview;
[Self.imgbrowseview registernib:[uinib nibwithnibname:@ "Custumcollectionviewcell" Bundle:nil] forcellwithreuseidentifier:@ "Cell"];
}
Demo:https://github.com/jiangtaidi/jwimagebrowsedemo.git
Run Result:
The above is the entire content of this article, I hope to help you learn.