• 无限滚动


    LWTViewController.m

    //
    //  LWTViewController.m
    //  无限滚动
    //
    //  Created by apple on 14-7-30.
    //  Copyright (c) 2014年 lwt. All rights reserved.
    //
    
    #import "LWTViewController.h"
    #import "LWTNews.h"
    #import "MJExtension.h"
    #import "LWTNewsCell.h"
    
    #define LWTCellIdentifier @"news"
    #define LWTMaxSection 100
    
    @interface LWTViewController () <UICollectionViewDataSource, UICollectionViewDelegate>
    @property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
    @property (nonatomic, strong) NSArray *newses;
    @property (nonatomic, weak) IBOutlet UIPageControl *pageContol;
    @property (nonatomic, strong) NSTimer *timer;
    @end
    
    @implementation LWTViewController
    
    -(NSArray *)newses
    {
        if (_newses == nil) {
            _newses = [LWTNews objectArrayWithFilename:@"newses.plist"];
            self.pageContol.numberOfPages = self.newses.count;
        }
        return _newses;
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // 注册cell
        [self.collectionView registerNib:[UINib nibWithNibName:@"LWTNewsCell" bundle:nil] forCellWithReuseIdentifier:LWTCellIdentifier];
        
        // 默认显示最中间的那组
        [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:LWTMaxSection / 2] atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
        
        // 添加定时器
        [self addTimer];
        
    }
    /**
     *  添加定时器
     */
    - (void)addTimer
    {
        NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
        [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
        self.timer = timer;
    }
    
    /**
     *  移除定时器
     */
    - (void)stopTimer
    {
        [self.timer invalidate];
        self.timer = nil;
    }
    
    - (NSIndexPath *)resetIndexPath
    {
        // 当前正在展示的位置
        NSIndexPath *currentIndexPath = [[self.collectionView indexPathsForVisibleItems] lastObject];
        
        // 马上显示回最中间那组的数据
        NSIndexPath *currentIndexPathReset = [NSIndexPath indexPathForItem:currentIndexPath.item inSection:LWTMaxSection / 2];
        [self.collectionView scrollToItemAtIndexPath:currentIndexPathReset atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
        
        return currentIndexPathReset;
    }
    
    /**
     *  下一页
     */
    - (void)nextPage
    {
        // 1.马上显示回最中间那组的数据
        NSIndexPath *indexPath = [self resetIndexPath];
        
         // 2.计算出下一个需要展示的位置
        NSInteger nextItem = indexPath.item + 1;
        NSInteger nextSection = indexPath.section;
        if (nextItem >= self.newses.count) {
            nextItem = 0;
            nextSection++;
        }
        
        // 3.通过动画滚动到下一个位置
        [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:nextItem inSection:nextSection] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
    }
    
    #pragma mark - UICollectionViewDataSource
    - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
    {
        return LWTMaxSection;
    }
    
    - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
    {
        return self.newses.count;
    }
    
    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {
        LWTNewsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:LWTCellIdentifier forIndexPath:indexPath];
        cell.news = self.newses[indexPath.item];
        return cell;
    }
    
    #pragma mark  - UICollectionViewDelegate
    /**
     *  当用户即将开始拖拽的时候就调用
     */
    - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
    {
        [self stopTimer];
    }
    
    - (void)scrollViewDidScroll:(UIScrollView *)scrollView
    {
        int page = (int)(scrollView.contentOffset.x / scrollView.bounds.size.width + 0.5) % self.newses.count;
        self.pageContol.currentPage = page;
    }
    /**
     *  当用户停止拖拽的时候就调用
     */
    - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
    {
        [self addTimer];
    }
    
    @end
    View Code
  • 相关阅读:
    文件名中含有空格读取时产生的异常
    R 常用清洗函数汇总
    Fluid 0.4 新版本正式发布:支持数据预热,优化小文件场景
    阿里云 Serverless 再升级,从体验上拉开差距
    Dubbo-go 源码笔记(二)客户端调用过程
    高质量的缺陷分析:让自己少写 bug
    微服务框架 Go-Micro 集成 Nacos 实战之服务注册与发现
    OpenYurt 深度解读:如何构建 Kubernetes 原生云边高效协同网络?
    在大规模 Kubernetes 集群上实现高 SLO 的方法
    双十一购物节,Nacos 1.4.0 + Go SDK 1.0.1发布
  • 原文地址:https://www.cnblogs.com/wentianblog/p/3878756.html
Copyright © 2020-2023  润新知