iOS自訂UICollectionViewFlowLayout實現圖片瀏覽效果_IOS

來源:互聯網
上載者:User

以前瀑布流的時候使用過UICollectionView,但是那時使用的是系統內建的UICollectionViewFlowLayout布局,今天看文章,看到UICollectionViewFlowLayout自訂相關的東西,於是動手寫了一個簡單圖片瀏覽的demo,熟練一些UICollectionViewFlowLayout自訂布局。

#import <UIKit/UIKit.h>@interface JWCollectionViewFlowLayout : UICollectionViewFlowLayout@end

自訂UICollectionViewFlowLayout,首先繼承UICollectionViewFlowLayout,實現一下幾個方法

#define screenWidth [UIScreen mainScreen].bounds.size.width#define MaxChangeRange 100#import "JWCollectionViewFlowLayout.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 isAtRight = 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.center.x;  if (ABS(itemHorizontalCenter - hCenter) < ABS(offset))  {   offset = itemHorizontalCenter - hCenter;  } } return CGPointMake(proposedContentOffset.x + offset, proposedContentOffset.y);}

使用

-(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

運行結果:

以上就是本文的全部內容,希望對大家的學習有所協助。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.