iOS開發之實現無限滑動
阿新 • • 發佈:2019-02-03
在看到這個標題的時候,相信大家心裡肯定會想,無限迴圈輪播的部落格已經滿天飛了,好有必要寫麼。這裡我想宣告一下,這裡的無線滑動,但是資料卻不迴圈。
實現原理
由於業務的需求,需要有大量的資料呈現在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
實現動畫效果如下:
實現還不完美,歡迎大家多多提建議哦
更多優質文章,可以微信掃碼關注: