欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

利用iOS實(shí)現(xiàn)系統(tǒng)相冊(cè)大圖瀏覽功能詳解

 更新時(shí)間:2017年09月19日 08:33:38   作者:laona  
查看大圖是們?nèi)粘i_(kāi)發(fā)中經(jīng)常會(huì)遇到的一個(gè)需求,下面這篇文章主要給大家介紹了關(guān)于利用iOS實(shí)現(xiàn)系統(tǒng)相冊(cè)大圖瀏覽功能的相關(guān)資料,文中給出了詳細(xì)的示例代碼供大家參考學(xué)習(xí),需要的朋友們下面來(lái)一起看看吧。

前言

本文主要給大家介紹了關(guān)于iOS實(shí)現(xiàn)系統(tǒng)相冊(cè)大圖瀏覽功能的相關(guān)資料,分享出來(lái)供大家參考學(xué)習(xí),下面話不多說(shuō)了,來(lái)一起看看詳細(xì)的介紹吧。

最終效果圖


大圖瀏覽

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

  • 創(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)通過(guò)兩個(gè)block來(lái)實(shí)現(xiàn)

-(void)collectionViewDidScrollWithScrollBlock:(DidScrollBlock)didScrollBlcok;
-(void)collectionViewDidScrollWithScrollBlock:(IndexScrollBlock)didScrollBlcok;

實(shí)現(xiàn)當(dāng)前顯示縮略圖放大通過(guò)設(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);
}

源碼下載:

Demo下載 (本地下載

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

最新評(píng)論