iOS實現(xiàn)無限滑動效果
在看到這個標題的時候,相信大家心里肯定會想,無限循環(huán)輪播的博客已經(jīng)滿天飛了,好有必要寫么。這里我想聲明一下,這里的無線滑動,但是數(shù)據(jù)卻不循環(huán)。
實現(xiàn)原理
由于業(yè)務的需求,需要有大量的數(shù)據(jù)呈現(xiàn)在collectionView上,但是又不想刷新全部的數(shù)據(jù),因此需要制定collectionView的cell的數(shù)量為有限的。針對這一種情況,我們需要保證頁面刷新數(shù)據(jù)源的索引和頁面滑動的索引是不致的。同時滑動停止后,悄悄的將collectionView恢復到初始的位置。
具體源碼如下:
@interface JKReadViewController ()<UIScrollViewDelegate> { ? ? NSArray *_datas; } @property (nonatomic,assign) ?NSInteger currentIndex; @property (nonatomic,assign) NSInteger cellCount; @property (nonatomic,assign) NSInteger sectionNum; @end @implementation JKReadViewController - (UICollectionViewFlowLayout *)collectionViewLayout{ ? ? UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init]; ? ? flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal; ? ? return flowLayout; } - (Class)cellClass{ ? ? return [JKPageCollectionCell class]; } - (void)viewDidLoad { ? ? [super viewDidLoad]; ? ? // Do any additional setup after loading the view. } - (void)configOrigin{ ? ? self.sectionNum = floor(self.dataIndex/self.cellCount); ? ? self.currentIndex = 1;//當前CollectionView的索引 ? ? NSIndexPath *idxPath = [NSIndexPath indexPathForItem:1 inSection:0]; ? ? [self.collectionView scrollToItemAtIndexPath:idxPath atScrollPosition:0 animated:NO]; } - (void)viewDidAppear:(BOOL)animated{ ? ? [super viewDidAppear:animated]; ? ? [self configOrigin]; } - (void)configUI{ ? ? [super configUI]; ? ? self.collectionView.pagingEnabled = YES; ? ? self.collectionView.showsHorizontalScrollIndicator = NO; ? ? self.collectionView.bounces = NO; } -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ ? ? NSInteger index = scrollView.contentOffset.x/ scrollView.bounds.size.width; ? ? if (index>self.currentIndex) { ? ? ? ? self.dataIndex++;//數(shù)據(jù)源的索引 ? ? }else if (index< self.currentIndex){ ? ? ? ? self.dataIndex--; ? ? ? ? self.dataIndex = self.dataIndex<0?0:self.dataIndex; ? ? } ? ? NSIndexPath *indexPath = [NSIndexPath indexPathForItem:1 inSection:0]; ? ? [self.collectionView reloadItemsAtIndexPaths:@[indexPath]]; ? ? dispatch_async(dispatch_get_main_queue(), ^{ ? ? ? ? [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:0 animated:NO]; ? ? }); } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath { ? ? JKPageCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[JKPageCollectionCell CellIndentifier] forIndexPath:indexPath]; ? ? ? ? ?NSString *title = self.datas[self.dataIndex]; ? ? ? ? [cell updateViewWithModel:title]; ? ? return cell; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ ? ? return self.cellCount; } - (NSArray *)datas{//模擬的大量的數(shù)據(jù)源 ? ? if (!_datas) { ? ? ? ? NSMutableArray *tempArray = [NSMutableArray new]; ? ? ? ? for (NSInteger i = 0; i< 1000; i++) { ? ? ? ? ? ? NSString *string = [NSString stringWithFormat:@"%@",@(i)]; ? ? ? ? ? ? [tempArray addObject:string]; ? ? ? ? } ? ? ? ? _datas = [tempArray copy]; ? ? } ? ? return _datas; } - (NSInteger)cellCount{ ? ? return 3;//單元格的數(shù)量 } - (void)didReceiveMemoryWarning { ? ? [super didReceiveMemoryWarning]; ? ? // Dispose of any resources that can be recreated. } @end
實現(xiàn)動畫效果如下:
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- IOS仿今日頭條滑動導航欄
- ios scrollview嵌套tableview同向滑動的示例
- iOS使用pageViewController實現(xiàn)多視圖滑動切換
- iOS滑動解鎖、滑動獲取驗證碼效果的實現(xiàn)代碼
- 詳解iOS中position:fixed吸底時的滑動出現(xiàn)抖動的解決方案
- 微信小程序在ios下Echarts圖表不能滑動的問題解決
- 微信瀏覽器彈出框滑動時頁面跟著滑動的實現(xiàn)代碼(兼容Android和IOS端)
- iOS 頁面滑動與標題切換顏色漸變的聯(lián)動效果實例
- IOS開發(fā)中禁止NavigationController的向右滑動返回
- iOS開發(fā)上下滑動UIScrollview隱藏或者顯示導航欄的實例
相關文章
iOS如何跳轉(zhuǎn)到App Store下載評分頁面示例代碼
最近在工作中遇到一個需求,需要跳轉(zhuǎn)到App Store下載評分,通過查找相關的資料最終解決了,下面這篇文章主要給大家介紹了關于iOS如何跳轉(zhuǎn)到App Store下載評分頁面的相關資料,需要的朋友可以參考借鑒,下面來一起看看吧。2017-12-12IOS UIWebView獲取404、504等錯誤問題解決方案
這篇文章主要介紹了IOS UIWebView獲取404、504等錯誤問題的相關資料,并對相應的錯誤問題提出相應的解決方案,需要的朋友可以參考下2016-11-11Objective-C優(yōu)雅使用KVO觀察屬性值變化
這篇文章主要為大家介紹了Objective-C優(yōu)雅使用KVO觀察屬性值變化示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-08-08iOS開發(fā)中不合法的網(wǎng)絡請求地址如何解決
這篇文章主要介紹了iOS開發(fā)中不合法的網(wǎng)絡請求地址的解決方案,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-09-09IOS CoreAnimation中l(wèi)ayer動畫閃爍的解決方法
這篇文章主要為大家詳細介紹了IOS CoreAnimation中l(wèi)ayer動畫閃爍的原因,分享了layer動畫閃爍的解決方法,感興趣的小伙伴們可以參考一下2016-06-06iOS開發(fā)WebViewJavascriptBridge通訊原理解析
這篇文章主要為大家介紹了iOS開發(fā)WebViewJavascriptBridge通訊原理示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11