iOS自定義UICollectionViewFlowLayout實(shí)現(xiàn)圖片瀏覽效果
以前瀑布流的時(shí)候使用過(guò)UICollectionView,但是那時(shí)使用的是系統(tǒng)自帶的UICollectionViewFlowLayout布局,今天看文章,看到UICollectionViewFlowLayout自定義相關(guān)的東西,于是動(dòng)手寫(xiě)了一個(gè)簡(jiǎn)單圖片瀏覽的demo,熟練一些UICollectionViewFlowLayout自定義布局。
#import <UIKit/UIKit.h> @interface JWCollectionViewFlowLayout : UICollectionViewFlowLayout @end
自定義UICollectionViewFlowLayout,首先繼承UICollectionViewFlowLayout,實(shí)現(xiàn)一下幾個(gè)方法
#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
運(yùn)行結(jié)果:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。
相關(guān)文章
iOS開(kāi)發(fā)實(shí)現(xiàn)計(jì)算器功能
這篇文章主要為大家詳細(xì)介紹了iOS開(kāi)發(fā)實(shí)現(xiàn)計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10iOS 中KVC、KVO、NSNotification、delegate 總結(jié)及區(qū)別
這篇文章主要介紹了iOS 中KVC、KVO、NSNotification、delegate 總結(jié)及區(qū)別的相關(guān)資料,需要的朋友可以參考下2016-10-10利用iOS開(kāi)發(fā)實(shí)現(xiàn)翻轉(zhuǎn)撲克牌動(dòng)畫(huà)的方法
這篇文章主要給大家介紹了關(guān)于利用iOS開(kāi)發(fā)實(shí)現(xiàn)翻撲克牌動(dòng)畫(huà)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)跟著小編一起學(xué)習(xí)學(xué)習(xí)吧。2017-07-07IOS使用progssview仿滴滴打車(chē)圓形計(jì)時(shí)
本文給大家分享的是IOS中實(shí)現(xiàn)仿滴滴打車(chē)的原型計(jì)時(shí)效果,非常的實(shí)用,有需要的小伙伴可以參考下。2015-07-07學(xué)習(xí)iOS自定義導(dǎo)航控制器UINavigationController
這篇文章主要為大家詳細(xì)介紹了iOS自定義導(dǎo)航控制器UINavigationController,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09iOS App使用SQLite之句柄的定義及數(shù)據(jù)庫(kù)的基本操作
SQLite中在定義過(guò)句柄之后就可以指向數(shù)據(jù)庫(kù),從而利用iOS應(yīng)用程序進(jìn)行打開(kāi)或關(guān)閉等各種操作,這里我們就來(lái)看一下iOS App使用SQLite之句柄的定義及數(shù)據(jù)庫(kù)的基本操作2016-06-06iOS開(kāi)發(fā)中class和#import的區(qū)別介紹
這篇文章主要介紹了iOS開(kāi)發(fā)中class和#import的區(qū)別,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2018-02-02OpenCV??iOS?圖像處理編程入門(mén)詳細(xì)教程
這篇文章主要介紹了OpenCV?iOS?圖像處理編程入門(mén),OpenCV?的應(yīng)用領(lǐng)域非常廣泛,包括圖像拼接、圖像降噪、產(chǎn)品質(zhì)檢、人機(jī)交互、人臉識(shí)別、動(dòng)作識(shí)別、動(dòng)作跟蹤、無(wú)人駕駛等,對(duì)于圖像處理、人機(jī)交互及機(jī)器學(xué)習(xí)算法感興趣的可以選擇一個(gè)方向進(jìn)行深入的研究2022-07-07ios實(shí)現(xiàn)底部PopupWindow的示例代碼(底部彈出菜單)
這篇文章主要介紹了ios實(shí)現(xiàn)底部PopupWindow的示例代碼(底部彈出菜單),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01