iOS实现无限滑动效果
作者:JackLee18
这篇文章主要为大家详细介绍了iOS实现无限滑动效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
在看到这个标题的时候,相信大家心里肯定会想,无限循环轮播的博客已经满天飞了,好有必要写么。这里我想声明一下,这里的无线滑动,但是数据却不循环。
实现原理
由于业务的需求,需要有大量的数据呈现在collectionView上,但是又不想刷新全部的数据,因此需要制定collectionView的cell的数量为有限的。针对这一种情况,我们需要保证页面刷新数据源的索引和页面滑动的索引是不致的。同时滑动停止后,悄悄的将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++;//数据源的索引 }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
实现动画效果如下:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。