您好,登錄后才能下訂單哦!
本篇內容主要講解“ios UICollectionView怎么實現圖片瀏覽效果”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“ios UICollectionView怎么實現圖片瀏覽效果”吧!
1.封裝 UICollectionViewLayout ,實現內部 UICollectionViewCell 的布局。
UICollectionViewLayout 在封裝瀑布流的時候會用到,而且擔負著核心功能的實現。其實從另一個角度也可以把 UICollectionViewLayout 理解成“數據源”,這個數據不是 UI 的展示項,而是 UI 的尺寸項。在內部進行預計算 UICollectionViewCell 的 frame。
UICollectionView 是 UIScrollView的子類,只不過,它里面子控件通過“重用”機制實現了優化,一些復用的復雜邏輯還是扔給了系統處理。開發過程中只負責對 UICollectionViewLayout 什么時候需要干什么進行自定義即可。
2.獲取 UICollectionView 目前可見的 cells,通過進行縮放、旋轉變換實現一些簡單的效果。
3、自定義 cell ,修改錨點屬性。
這里有一點需要注意的,在 UICollectionViewLayout 內部會進行計算每一個 cell 的 frame,在計算過程中,為了更好的展示旋轉變換,cell 的錨點會修改到 (0.5,1),那么,為了保證 UI 展示不變,那么,就需要將 y 增加 cell 高度的一半。
#import "PhotoBrowseViewLayout.h" @interface PhotoBrowseViewLayout() @property(nonatomic,strong) NSMutableArray * attributeArray; @property(nonatomic,assign) CGFloat cellWidth; @property(nonatomic,assign) CGFloat cellHeight; @property(nonatomic,assign) CGFloat sep; @property(nonatomic,assign) int showCellNum; @end @implementation PhotoBrowseViewLayout - (instancetype)init { if (self = [super init]) { self.sep = 20; self.showCellNum = 2; } return self; } //計算cell的frame - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath { if (self.cellWidth == 0) { self.cellWidth = **self**.collectionView.frame.size.width * 2 / 3.0; } if (self.cellHeight == 0) { self.cellHeight = self.collectionView.frame.size.height; } CGFloat x = (self.cellWidth + self.sep) * indexPath.item; //這里y值需要進行如此設置,以抵抗cell修改錨點導致的UI錯亂 CGFloat y = self.collectionView.frame.size.height / 2.0; UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath]; attrs.frame = CGRectMake(x, y, self.cellWidth, self.cellHeight); return attrs; } //準備布局 - (void)prepareLayout { [super prepareLayout]; NSInteger count = [self.collectionView numberOfItemsInSection:0]; for (int i = 0; i <count; i++) { UICollectionViewLayoutAttributes *attris = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]]; [self.attributeArray addObject:attris]; } } //返回全部cell的布局集合 - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect { return self.attributeArray; } //一次性提供UICollectionView 的 contentSize - (CGSize)collectionViewContentSize { NSInteger count = [self.collectionView numberOfItemsInSection:0]; CGFloat maxWidth = count * self.cellWidth + (count - 1) * self.sep; return CGSizeMake(maxWidth, 0); } - (NSMutableArray *)attributeArray { if (!_attributeArray) { _attributeArray = [[NSMutableArray alloc] init]; } return _attributeArray; } @end
這里主要是進行了錨點修改(0.5,1),代碼很簡單。
#import "PhotoBrowseCollectionViewCell.h" @interface PhotoBrowseCollectionViewCell() @property(nonatomic,strong) UIImageView * imageView; @end @implementation PhotoBrowseCollectionViewCell - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { //設置(0.5,1)錨點,以底部中點為軸旋轉 self.layer.anchorPoint = CGPointMake(0.5, 1); self.layer.masksToBounds = YES; self.layer.cornerRadius = 8; } return self; } - (void)setImage:(UIImage *)image { self.imageView.image = image; } - (UIImageView *)imageView { if (!_imageView) { _imageView = [[UIImageView alloc] init]; _imageView.contentMode = UIViewContentModeScaleAspectFill; _imageView.backgroundColor = [UIColor groupTableViewBackgroundColor]; [self.contentView addSubview:_imageView]; } return _imageView; } - (void)layoutSubviews { [super layoutSubviews]; self.imageView.frame = **self**.contentView.bounds; } @end
CollectPhotoBrowseView 負責進行一些 cell 的圖形變換。
#import "CollectPhotoBrowseView.h" #import "PhotoBrowseCollectionViewCell.h" #import "PhotoBrowseViewLayout.h" @interface CollectPhotoBrowseView()<UICollectionViewDelegate,UICollectionViewDataSource> @property(nonatomic,strong) UICollectionView * photoCollectView; @end @implementation CollectPhotoBrowseView - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { [self makeUI]; } return self; } - (void)makeUI{ //設置自定義 UICollectionViewLayout PhotoBrowseViewLayout * photoBrowseViewLayout = [[PhotoBrowseViewLayout alloc] init]; self.photoCollectView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:photoBrowseViewLayout]; self.photoCollectView.delegate = self; self.photoCollectView.dataSource = self; [self.photoCollectView registerClass:[PhotoBrowseCollectionViewCell class] forCellWithReuseIdentifier:@"CELL"]; self.photoCollectView.showsHorizontalScrollIndicator = NO; [self addSubview:self.photoCollectView]; //執行一次可見cell的圖形變換 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [self visibleCellTransform]; }); } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return 20; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { PhotoBrowseCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CELL" forIndexPath:indexPath]; [cell setImage: [UIImage imageNamed:[NSString stringWithFormat:@"fd%ld",indexPath.item % 3 + 1]]]; return cell; } #pragma mark - 滾動進行圖形變換 - (void)scrollViewDidScroll:(UIScrollView *)scrollView { //滑動的時候,動態進行cell圖形變換 [self visibleCellTransform]; } #pragma mark - 圖形變化 - (void)visibleCellTransform { //獲取當前可見cell的indexPath集合 NSArray * visibleItems = [self.photoCollectView indexPathsForVisibleItems]; //遍歷動態進行圖形變換 for (NSIndexPath * visibleIndexPath in visibleItems) { UICollectionViewCell * visibleCell = [self.photoCollectView cellForItemAtIndexPath:visibleIndexPath]; [self transformRotateWithView:visibleCell]; } } //進行圖形轉換 - (void)transformRotateWithView:(UICollectionViewCell *)cell { //獲取cell在當前視圖的位置 CGRect rect = [cell convertRect:cell.bounds toView:self]; //計算當前cell中軸線與中軸線的距離的比值 float present = ((CGRectGetMidX(rect) - self.center.x) / (self.frame.size.width / 2.0)); //根據位置設置選擇角度 CGFloat radian = (M_PI_2 / 15) * present; //圖形角度變換 CGAffineTransform transformRotate = CGAffineTransformIdentity; transformRotate = CGAffineTransformRotate(transformRotate, radian); //圖形縮放變換 CGAffineTransform transformScale = CGAffineTransformIdentity transformScale = CGAffineTransformScale(transformScale,1 - 0.2 * fabs(present),1 - 0.2 * fabsf(present)); //合并變換 cell.transform = CGAffineTransformConcat(transformRotate,transformScale); } @end
到此,相信大家對“ios UICollectionView怎么實現圖片瀏覽效果”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。