iOS實現無限滑動效果
更新時間:2022年03月21日 10:29:59 作者:JackLee18
這篇文章主要為大家詳細介紹了iOS實現無限滑動效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
在看到這個標題的時候,相信大家心里肯定會想,無限循環(huán)輪播的博客已經滿天飛了,好有必要寫么。這里我想聲明一下,這里的無線滑動,但是數據卻不循環(huán)。
實現原理
由于業(yè)務的需求,需要有大量的數據呈現在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實現動畫效果如下:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- IOS仿今日頭條滑動導航欄
- ios scrollview嵌套tableview同向滑動的示例
- iOS使用pageViewController實現多視圖滑動切換
- iOS滑動解鎖、滑動獲取驗證碼效果的實現代碼
- 詳解iOS中position:fixed吸底時的滑動出現抖動的解決方案
- 微信小程序在ios下Echarts圖表不能滑動的問題解決
- 微信瀏覽器彈出框滑動時頁面跟著滑動的實現代碼(兼容Android和IOS端)
- iOS 頁面滑動與標題切換顏色漸變的聯(lián)動效果實例
- IOS開發(fā)中禁止NavigationController的向右滑動返回
- iOS開發(fā)上下滑動UIScrollview隱藏或者顯示導航欄的實例
相關文章
IOS UI學習教程之使用代碼創(chuàng)建button
這篇文章主要為大家詳細介紹了IOS UI學習教程之使用代碼創(chuàng)建button,感興趣的小伙伴們可以參考一下2016-03-03

