iOS_无限滚动

Github上查看并下载源代码地址
最终效果图:

使用CollectionView实现,带pageContrl + timer定时器
思路,使用1组,但是告诉控制器有modelArrCount*5000个item,并且cellForRow时,创建根据index取模modelArrCount,取出数据源(实际只有8个),并且item的宽度就是一个屏幕的宽度
代码片段:
</pre><pre>
 
  
#import "BeyondViewController.h"
// 快速Frame
#import "UIView+Frame.h"

#import "BeyondNewsCell.h"
#import "BeyondNews.h"
#import "MJExtension.h"

// 只有一组,但是该组 有5000*8行
#define TotalItems (5000 * self.newses.count)
// 第一次出现的时候,didAppear时,就出现在中间,左边对齐
#define MiddleItem (NSUInteger)(TotalItems * 0.5)

@interface BeyondViewController () <UICollectionViewDataSource, UICollectionViewDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UIPageControl *pageCtrl;
@property (strong, nonatomic) NSArray *newses;
@property (strong, nonatomic) NSTimer *timer;
@end

@implementation BeyondViewController
#pragma mark - 懒加载
- (NSArray *)newses
{
    if (!_newses) {
        self.newses = [BeyondNews objectArrayWithFilename:@"newses.plist"];
        // 总页数
        self.pageCtrl.numberOfPages = self.newses.count;
    }
    return _newses;
}
#pragma mark - 生命周期
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 注册cell【UICollectionViewCell不让代码创建】
    [self.collectionView registerNib:[UINib nibWithNibName:@"BeyondNewsCell" bundle:nil] forCellWithReuseIdentifier:@"news"];
    
    // 添加定时器
    [self addTimer];
    
}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:MiddleItem inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
}
#pragma mark - 时钟方法
- (void)addTimer
{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(schedule) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}
- (void)removeTimer
{
    [self.timer invalidate];
    self.timer = nil;
}
// 定时器方法
- (void)schedule
{
    // 得到当前显示的item
    NSIndexPath *visiablePath = [[self.collectionView indexPathsForVisibleItems] firstObject];
    
    NSUInteger visiableItem = visiablePath.item;
    if ((visiablePath.item % self.newses.count)  == 0) { // 第0张图片
        [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:MiddleItem inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
        visiableItem = MiddleItem;
    }
    
    // 滚动到下一个item
    NSUInteger nextItem = visiableItem + 1;
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:nextItem inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
}
#pragma mark - 数据源和代理方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return TotalItems;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"news";
    // 直接取
    BeyondNewsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    // UICollectionViewCell没有根据id进行创建方法,只能 从xib加载
    //    UICollectionViewFlowLayout *layout 中 可以指定cell的高度
    
    cell.news = self.newses[indexPath.item % self.newses.count];
    
    return cell;
}
// 重要,监听代理 停止滚动的事件
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSIndexPath *visiablePath = [[collectionView indexPathsForVisibleItems] firstObject];
    self.pageCtrl.currentPage = visiablePath.item % self.newses.count;
}

#pragma mark - scrollView代理
// 开始拖拽
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self removeTimer];
}
// 停止拖拽
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self addTimer];
}
@end


使用到的分类:
//
//  Frame.h
//  08-无限滚动
//
//  Created by beyond on 15-3-27.
//  Copyright (c) 2015年 itcast. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface UIView (Frame)
@property (assign, nonatomic) CGFloat x;
@property (assign, nonatomic) CGFloat y;
@property (assign, nonatomic) CGFloat width;
@property (assign, nonatomic) CGFloat height;
@property (assign, nonatomic) CGSize size;
@property (assign, nonatomic) CGPoint origin;
@end


//
//  Frame.m
//  08-无限滚动
//
//  Created by beyond on 15-3-27.
//  Copyright (c) 2015年 itcast. All rights reserved.
//

#import "UIView+Frame.h"

@implementation UIView (Frame)
- (void)setX:(CGFloat)x
{
    CGRect frame = self.frame;
    frame.origin.x = x;
    self.frame = frame;
}

- (CGFloat)x
{
    return self.frame.origin.x;
}

- (void)setY:(CGFloat)y
{
    CGRect frame = self.frame;
    frame.origin.y = y;
    self.frame = frame;
}

- (CGFloat)y
{
    return self.frame.origin.y;
}

- (void)setWidth:(CGFloat)width
{
    CGRect frame = self.frame;
    frame.size.width = width;
    self.frame = frame;
}

- (CGFloat)width
{
    return self.frame.size.width;
}

- (void)setHeight:(CGFloat)height
{
    CGRect frame = self.frame;
    frame.size.height = height;
    self.frame = frame;
}

- (CGFloat)height
{
    return self.frame.size.height;
}

- (void)setSize:(CGSize)size
{
    CGRect frame = self.frame;
    frame.size = size;
    self.frame = frame;
}

- (CGSize)size
{
    return self.frame.size;
}

- (void)setOrigin:(CGPoint)origin
{
    CGRect frame = self.frame;
    frame.origin = origin;
    self.frame = frame;
}

- (CGPoint)origin
{
    return self.frame.origin;
}
@end


MainStoryBoard,在FlowLayout中指定item的size

Xib创建CollectionCell,注意指定重用ID


你可能感兴趣的:(ios,CollectionView,自动布局,autolayout,无限滚动)