iOS_無限滾動,iOS_滾動
最終:
使用CollectionView實現,帶pageContrl + timer定時器思路,使用1組,但是告訴控制器有modelArrCount*5000個item,並且cellForRow時,建立根據index模數modelArrCount,取出資料來源(實際只有8個),並且item的寬度就是一個螢幕的寬度程式碼片段:
#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