利用iOS實(shí)現(xiàn)系統(tǒng)相冊(cè)大圖瀏覽功能詳解
前言
本文主要給大家介紹了關(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); }
源碼下載:
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
iOS開(kāi)發(fā)中實(shí)現(xiàn)一個(gè)簡(jiǎn)單的圖片瀏覽器的實(shí)例講解
這篇文章主要介紹了iOS開(kāi)發(fā)中實(shí)現(xiàn)一個(gè)簡(jiǎn)單的圖片瀏覽器的實(shí)例講解,代碼基礎(chǔ)傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-01-01詳解Objective-C設(shè)計(jì)模式編程中對(duì)備忘錄模式的運(yùn)用
這篇文章主要介紹了Objective-C設(shè)計(jì)模式編程中對(duì)備忘錄模式的運(yùn)用,文中結(jié)合了Cocoa框架下應(yīng)用的實(shí)例來(lái)加以講解,需要的朋友可以參考下2016-03-03iOS應(yīng)用開(kāi)發(fā)中使用Auto Layout來(lái)適配不同屏幕尺寸
這篇文章主要介紹了iOS應(yīng)用開(kāi)發(fā)中使用Auto Layout來(lái)適配不同屏幕尺寸的方法,根據(jù)Xcode IDE下的實(shí)際調(diào)試步驟講解其用法,需要的朋友可以參考下2016-03-03iOS開(kāi)發(fā)中使用FMDB來(lái)使程序連接SQLite數(shù)據(jù)庫(kù)
這篇文章主要介紹了iOS開(kāi)發(fā)中使用FMDB來(lái)使程序連接SQLite數(shù)據(jù)庫(kù),SQLite是一個(gè)簡(jiǎn)單的嵌入式數(shù)據(jù)庫(kù),非常適合輕量級(jí)使用,需要的朋友可以參考下2015-11-11iOS開(kāi)發(fā)之Quartz2D的介紹與使用詳解
什么是Quartz2D?Quartz 2D是一個(gè)二維繪圖引擎,同時(shí)支持iOS和Mac系統(tǒng)。下面這篇文章主要介紹了iOS開(kāi)發(fā)之Quartz2D的介紹與使用的相關(guān)資料,需要的朋友可以參考下2017-03-03iOS開(kāi)發(fā)教程之XLForm的基本使用方法
XLForm 是最靈活且最強(qiáng)大的創(chuàng)建動(dòng)態(tài)表單的iOS庫(kù),下面這篇文章主要給大家介紹了關(guān)于iOS開(kāi)發(fā)教程之XLForm的基本使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-04-04iOS16使用SwiftUI Charts創(chuàng)建折線圖實(shí)現(xiàn)實(shí)例
這篇文章主要為大家介紹了iOS16使用SwiftUI Charts創(chuàng)建折線圖實(shí)現(xiàn)實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11