IOS custom Uicollectionviewflowlayout To achieve image browsing effect _ios

Source: Internet
Author: User
Tags abs uikit

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.