網站首頁 編程語言 正文
在看到這個標題的時候,相信大家心里肯定會想,無限循環輪播的博客已經滿天飛了,好有必要寫么。這里我想聲明一下,這里的無線滑動,但是數據卻不循環。
實現原理
由于業務的需求,需要有大量的數據呈現在collectionView上,但是又不想刷新全部的數據,因此需要制定collectionView的cell的數量為有限的。針對這一種情況,我們需要保證頁面刷新數據源的索引和頁面滑動的索引是不致的。同時滑動停止后,悄悄的將collectionView恢復到初始的位置。
具體源碼如下:
@interface JKReadViewController (){ ? ? 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++;//數據源的索引 ? ? }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{//模擬的大量的數據源 ? ? 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;//單元格的數量 } - (void)didReceiveMemoryWarning { ? ? [super didReceiveMemoryWarning]; ? ? // Dispose of any resources that can be recreated. } @end
實現動畫效果如下:
原文鏈接:https://blog.csdn.net/hanhailong18/article/details/81138554
相關推薦
- 2022-02-03 Windows10 可以上網(會斷)顯示無Internet鏈接
- 2022-06-21 python使用matplotlib繪制折線圖_python
- 2022-07-18 CSS基礎語法和盒模型
- 2024-04-02 docker開機自啟設置
- 2022-04-09 Redis分布式鎖防止緩存擊穿的實現_Redis
- 2022-05-02 利用numba讓python速度提升百倍_python
- 2022-08-02 Android開發雙向滑動選擇器范圍SeekBar實現_Android
- 2022-04-20 基于PyQT5制作一個桌面摸魚工具_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支