網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
在看到這個(gè)標(biāo)題的時(shí)候,相信大家心里肯定會(huì)想,無(wú)限循環(huán)輪播的博客已經(jīng)滿天飛了,好有必要寫(xiě)么。這里我想聲明一下,這里的無(wú)線滑動(dòng),但是數(shù)據(jù)卻不循環(huán)。
實(shí)現(xiàn)原理
由于業(yè)務(wù)的需求,需要有大量的數(shù)據(jù)呈現(xiàn)在collectionView上,但是又不想刷新全部的數(shù)據(jù),因此需要制定collectionView的cell的數(shù)量為有限的。針對(duì)這一種情況,我們需要保證頁(yè)面刷新數(shù)據(jù)源的索引和頁(yè)面滑動(dòng)的索引是不致的。同時(shí)滑動(dòng)停止后,悄悄的將collectionView恢復(fù)到初始的位置。
具體源碼如下:
@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;//當(dāng)前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
實(shí)現(xiàn)動(dòng)畫(huà)效果如下:
原文鏈接:https://blog.csdn.net/hanhailong18/article/details/81138554
相關(guān)推薦
- 2024-03-07 使用JdbcTemplate和Druid技術(shù)簡(jiǎn)化持久層的編寫(xiě)
- 2024-01-16 linux查看外網(wǎng)ip的5種方法
- 2022-12-26 Python中pywifi模塊的基本用法講解_python
- 2022-08-16 Python利用fastapi實(shí)現(xiàn)上傳文件_python
- 2022-10-19 R語(yǔ)言初學(xué)者的一些常見(jiàn)報(bào)錯(cuò)指南_R語(yǔ)言
- 2022-06-11 C#實(shí)現(xiàn)文件Move和Copy操作_C#教程
- 2021-12-10 antd react hooks From表單格式模板
- 2022-10-04 python中xml格式的轉(zhuǎn)換方法_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支