美女.jpg
實現(xiàn)過程
- 創(chuàng)建兩個UICollectionView分別放置大圖和縮略圖
- 實現(xiàn)大圖和縮略圖的聯(lián)動
- 實現(xiàn)當(dāng)前展示的大圖對應(yīng)的縮略圖放大效果
實現(xiàn)原理
- 創(chuàng)建collectionView非常簡單樊拓,只要正常創(chuàng)建就好龙亲,值得注意的是:由于需要當(dāng)前展示的圖片的縮略圖始終保持在屏幕中間位置怕享,所以在創(chuàng)建縮略圖的collectionView的時候需要對collection View設(shè)置好便宜量。
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
return UIEdgeInsetsMake(0, (kScreenWidth - _itemWidth)/2, 0, (kScreenWidth - _itemWidth)/2);
} - 實現(xiàn)上下聯(lián)動通過兩個block來實現(xiàn)
-(void)collectionViewDidScrollWithScrollBlock:(DidScrollBlock)didScrollBlcok;
-(void)collectionViewDidScrollWithScrollBlock:(IndexScrollBlock)didScrollBlcok;
- 實現(xiàn)當(dāng)前顯示縮略圖放大通過設(shè)置collectionView的UICollectionViewFlowLayout實現(xiàn)
IndexFlow *flowLayout = [[IndexFlow alloc] init];
self = [super initWithFrame:frame collectionViewLayout:flowLayout];
部分實現(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;
}
縮略圖放大實現(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動那一刻的位置
*
* @param proposedContentOffset 原本collectionView停止?jié)L動那一刻的位置
* @param velocity 速度
*
* @return 最終的位置
*/
- (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity
{
CGFloat offsetAdjustment = MAXFLOAT;
CGFloat horizontalCenter = proposedContentOffset.x + (CGRectGetWidth(self.collectionView.bounds) / 2.0);
// 停止時刻的可視區(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