iOS仿系統(tǒng)相冊(cè)大圖瀏覽功能

美女.jpg

Demo下載

實(shí)現(xiàn)過程

  • 創(chuàng)建兩個(gè)UICollectionView分別放置大圖和縮略圖
  • 實(shí)現(xiàn)大圖和縮略圖的聯(lián)動(dòng)
  • 實(shí)現(xiàn)當(dāng)前展示的大圖對(duì)應(yīng)的縮略圖放大效果

實(shí)現(xiàn)原理

  • 創(chuàng)建collectionView非常簡(jiǎn)單,只要正常創(chuàng)建就好,值得注意的是:由于需要當(dāng)前展示的圖片的縮略圖始終保持在屏幕中間位置,所以在創(chuàng)建縮略圖的collectionView的時(shí)候需要對(duì)collection View設(shè)置好便宜量。
    - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, (kScreenWidth - _itemWidth)/2, 0, (kScreenWidth - _itemWidth)/2);
    }
  • 實(shí)現(xiàn)上下聯(lián)動(dòng)通過兩個(gè)block來(lái)實(shí)現(xiàn)
    -(void)collectionViewDidScrollWithScrollBlock:(DidScrollBlock)didScrollBlcok;
    -(void)collectionViewDidScrollWithScrollBlock:(IndexScrollBlock)didScrollBlcok;
  • 實(shí)現(xiàn)當(dāng)前顯示縮略圖放大通過設(shè)置collectionView的UICollectionViewFlowLayout實(shí)現(xiàn)
    IndexFlow *flowLayout = [[IndexFlow alloc] init];
    self = [super initWithFrame:frame collectionViewLayout:flowLayout];

部分實(shí)現(xiàn)代碼

- (void)collectionViewDidScrollWithScrollBlock:(DidScrollBlock)didScrollBlcok{

self.didScrollBlcok = didScrollBlcok;
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{

self.shouldDid = YES;

return YES;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
self.shouldDid = NO;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
  if (self.shouldDid) {
    if (self.didScrollBlcok) {
        self.didScrollBlcok(self);
    }
  }
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
return UIEdgeInsetsMake(0, (kScreenWidth - _itemWidth)/2, 0,  (kScreenWidth - _itemWidth)/2);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
return CGSizeMake(_itemWidth, self.height);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
return _minLineSpacing;
}

縮略圖放大實(shí)現(xiàn)代碼
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)oldBounds
{
return YES;
}

-(NSArray*)layoutAttributesForElementsInRect:(CGRect)rect
{
// 取出所有元素
NSArray *array = [super layoutAttributesForElementsInRect:rect];
// 可視區(qū)域
CGRect visibleRect;
visibleRect.origin = self.collectionView.contentOffset;
visibleRect.size = self.collectionView.bounds.size;    
for (UICollectionViewLayoutAttributes *attribute in array) {
    CGFloat distance = CGRectGetMidX(visibleRect) - attribute.center.x;
    CGFloat normalizedDistance = distance / ACTIVE_DISTANCE;
    if (ABS(distance) < ACTIVE_DISTANCE) {
        CGFloat zoom = 1 + ZOOM_FACTOR*(1 - ABS(normalizedDistance));
        attribute.transform3D = CATransform3DMakeScale(zoom, zoom, 1.0);
        attribute.zIndex = 1;
    }
}
return array;
}

/**
 *  設(shè)置collectionView停止?jié)L動(dòng)那一刻的位置
 *
 *  @param proposedContentOffset 原本collectionView停止?jié)L動(dòng)那一刻的位置
 *  @param velocity              速度
 *
 *  @return 最終的位置
 */
- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity
{
CGFloat offsetAdjustment = MAXFLOAT;
CGFloat horizontalCenter = proposedContentOffset.x + (CGRectGetWidth(self.collectionView.bounds) / 2.0);
// 停止時(shí)刻的可視區(qū)域
CGRect targetRect = CGRectMake(proposedContentOffset.x, 0.0, self.collectionView.bounds.size.width, self.collectionView.bounds.size.height);
NSArray* array = [super layoutAttributesForElementsInRect:targetRect];
for (UICollectionViewLayoutAttributes* layoutAttributes in array) {
    CGFloat itemHorizontalCenter = layoutAttributes.center.x;
    if (ABS(itemHorizontalCenter - horizontalCenter) < ABS(offsetAdjustment)) {
        offsetAdjustment = itemHorizontalCenter - horizontalCenter;
    }
}
return CGPointMake(proposedContentOffset.x + offsetAdjustment, proposedContentOffset.y);
}

最終效果圖

大圖瀏覽.gif
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容