欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS實現(xiàn)無限滑動效果

 更新時間:2022年03月21日 10:29:59   作者:JackLee18  
這篇文章主要為大家詳細介紹了iOS實現(xiàn)無限滑動效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

在看到這個標題的時候,相信大家心里肯定會想,無限循環(huán)輪播的博客已經(jīng)滿天飛了,好有必要寫么。這里我想聲明一下,這里的無線滑動,但是數(shù)據(jù)卻不循環(huán)。

實現(xiàn)原理

由于業(yè)務的需求,需要有大量的數(shù)據(jù)呈現(xiàn)在collectionView上,但是又不想刷新全部的數(shù)據(jù),因此需要制定collectionView的cell的數(shù)量為有限的。針對這一種情況,我們需要保證頁面刷新數(shù)據(jù)源的索引和頁面滑動的索引是不致的。同時滑動停止后,悄悄的將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++;//數(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

實現(xiàn)動畫效果如下:

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • iOS如何跳轉(zhuǎn)到App Store下載評分頁面示例代碼

    iOS如何跳轉(zhuǎn)到App Store下載評分頁面示例代碼

    最近在工作中遇到一個需求,需要跳轉(zhuǎn)到App Store下載評分,通過查找相關的資料最終解決了,下面這篇文章主要給大家介紹了關于iOS如何跳轉(zhuǎn)到App Store下載評分頁面的相關資料,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-12-12
  • IOS UIWebView獲取404、504等錯誤問題解決方案

    IOS UIWebView獲取404、504等錯誤問題解決方案

    這篇文章主要介紹了IOS UIWebView獲取404、504等錯誤問題的相關資料,并對相應的錯誤問題提出相應的解決方案,需要的朋友可以參考下
    2016-11-11
  • Objective-C優(yōu)雅使用KVO觀察屬性值變化

    Objective-C優(yōu)雅使用KVO觀察屬性值變化

    這篇文章主要為大家介紹了Objective-C優(yōu)雅使用KVO觀察屬性值變化示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-08-08
  • iOS開發(fā)中不合法的網(wǎng)絡請求地址如何解決

    iOS開發(fā)中不合法的網(wǎng)絡請求地址如何解決

    這篇文章主要介紹了iOS開發(fā)中不合法的網(wǎng)絡請求地址的解決方案,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-09-09
  • IOS CoreAnimation中l(wèi)ayer動畫閃爍的解決方法

    IOS CoreAnimation中l(wèi)ayer動畫閃爍的解決方法

    這篇文章主要為大家詳細介紹了IOS CoreAnimation中l(wèi)ayer動畫閃爍的原因,分享了layer動畫閃爍的解決方法,感興趣的小伙伴們可以參考一下
    2016-06-06
  • iOS中的實時遠程配置全紀錄

    iOS中的實時遠程配置全紀錄

    這篇文章主要給大家介紹了關于iOS中實時遠程配置的相關資料,文中通過示例代碼介紹的非常詳細,對各位iOS開發(fā)者們具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-01-01
  • 詳細談談iOS字符串翻轉(zhuǎn)

    詳細談談iOS字符串翻轉(zhuǎn)

    字符串翻轉(zhuǎn)是我們在學習算法的時候經(jīng)常會遇到的一個基礎算題,下面這篇文章主要給大家詳解介紹了關于iOS字符串翻轉(zhuǎn)的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面來一起看看吧
    2018-07-07
  • 解決iOS下無法觸發(fā)focus事件的問題

    解決iOS下無法觸發(fā)focus事件的問題

    今天小編就為大家分享一篇解決iOS下無法觸發(fā)focus事件的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-08-08
  • 如何在 iOS 應用中添加位置信息

    如何在 iOS 應用中添加位置信息

    這篇文章主要介紹了如何在 iOS 應用中添加位置信息,幫助大家更好的理解和學習使用ios,感興趣的朋友可以了解下
    2021-02-02
  • iOS開發(fā)WebViewJavascriptBridge通訊原理解析

    iOS開發(fā)WebViewJavascriptBridge通訊原理解析

    這篇文章主要為大家介紹了iOS開發(fā)WebViewJavascriptBridge通訊原理示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-11-11

最新評論