实现思路:
* 开启一个定时器,把操做放入消息循环池.每隔必定时间,操做执行一次.
* 注意点: 程序启动自动轮播,手动拖拽,让定时器中止,中止拖拽从新开启一个定时器.安全
// // PBCollectionCell.h // 无限滚动 // // Created by 裴波波 on 16/3/30. // Copyright © 2016年 裴波波. All rights reserved. // #import <UIKit/UIKit.h> @interface PBCollectionCell : UICollectionViewCell @property(nonatomic,strong)UIImage * img; @end
// // PBCollectionCell.m // 无限滚动 // // Created by 裴波波 on 16/3/30. // Copyright © 2016年 裴波波. All rights reserved. // #import "PBCollectionCell.h" @interface PBCollectionCell () //不要忘记给collectionView的cell上设置图片框,并拖线到cell分类中 @property (strong, nonatomic) IBOutlet UIImageView *imgView; @end @implementation PBCollectionCell //重写img的set方法来个cell进行赋值.(当调用cell.img = img的时候回调用set ..img的方法) -(void)setImg:(UIImage *)img{ _img = img; self.imgView.image = img; } @end
// // ViewController.m // 无限滚动 // // Created by 裴波波 on 16/3/30. // Copyright © 2016年 裴波波. All rights reserved. // #import "ViewController.h" #import "PBCollectionCell.h" //定义宏图片的个数 #define kPictureCount 3 @interface ViewController () @property (strong, nonatomic) IBOutlet UICollectionViewFlowLayout *flowLayout; /** * 图片的索引 */ @property(nonatomic,assign) NSInteger index; //声明定时器属性方便在不一样方法中访问 @property(nonatomic,weak) NSTimer * timer; @end
@implementation ViewController -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ return kPictureCount; } -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ static NSString * ID = @"cell"; PBCollectionCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath]; //图片索引只有下一站或者上一张,即+1,或者-1,为了切换图片 //中间的cell的脚标是1,滚动后脚标是2或者0,凑成next值为+1或者-1(让图片索引+1或者-1来完成切换图片),则 NSInteger next = indexPath.item - 1; //为了避免让next越界,进行取余运算 ---------+next为了切换图片 next = (self.index + kPictureCount + next) % kPictureCount; NSString * imgName = [NSString stringWithFormat:@"%02ld",next + 1]; UIImage * img = [UIImage imageNamed:imgName]; cell.img = img; return cell; }
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ //计算偏移的整数倍,偏移了0或者是2, -1是让self.index图片索引+1 或者 -1以切换图片; NSInteger offset = scrollView.contentOffset.x / scrollView.bounds.size.width - 1; self.index = (offset + self.index + kPictureCount) % kPictureCount; //本质就是改变cell的索引,再根据self.index来切换图片,使用取余让图片索引不至于越界 //异步主队列执行,为了避免让连续滚动中止后,图片有闪动. dispatch_async(dispatch_get_main_queue(), ^{ [self scrollToSecondCell]; }); }
//封装设置当前显示的cell为第二个cell的方法. -(void)scrollToSecondCell{ NSIndexPath * idxPath = [NSIndexPath indexPathForItem:1 inSection:0]; [self.collectionView scrollToItemAtIndexPath:idxPath atScrollPosition:UICollectionViewScrollPositionLeft animated:NO]; } - (void)viewDidLoad { [super viewDidLoad]; self.flowLayout.itemSize = self.collectionView.bounds.size; self.flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal; self.flowLayout.minimumLineSpacing = 0; self.collectionView.backgroundColor = [UIColor whiteColor]; self.collectionView.pagingEnabled = YES; self.collectionView.bounces = NO; self.collectionView.showsHorizontalScrollIndicator = NO; [self scrollToSecondCell]; //开启定时器使程序启动就开始滚动 self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(nextPic) userInfo:nil repeats:YES]; NSRunLoop * runloop = [NSRunLoop currentRunLoop]; [runloop addTimer:self.timer forMode:NSRunLoopCommonModes]; } @end
#pragma mark - 每隔一秒执行一次切换图片 -(void)nextPic{ CGFloat w = 2 * self.collectionView.bounds.size.width; [self.collectionView setContentOffset:CGPointMake(w, 0) animated:YES]; }
#pragma mark - 手动开始拖拽中止定时器 // 开始拖拽 - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { // 1. 中止计时器 [self.timer invalidate]; }
#pragma mark - 中止拖拽开启定时器 // 结束拖拽 - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { // 从新开启一个计时器 self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(nextPic) userInfo:nil repeats:YES]; // 把self.timer加入消息循环的模式修改一下 NSRunLoop *runloop = [NSRunLoop currentRunLoop]; [runloop addTimer:self.timer forMode:NSRunLoopCommonModes]; }
-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{ NSInteger offset = scrollView.contentOffset.x / scrollView.bounds.size.width; //第二个cell一直在中间因此,滚动中止后偏移倍数为0 或者 2 ==offset offset = offset - 1; //cell的脚标self.index +1 或者 -1; self.index = (kPictureCount + offset + self.index) % kPictureCount; //将操做放入主队列异步执行,防止连续滚动中止后,图片错位后又变回正常显示的图片. dispatch_async(dispatch_get_main_queue(), ^{ [self scrollToSecondCell]; }); }
1. cell滑动前或者每次滑动结束后都用代码设置当前显示的cell为第二个cell,在数据源第三个方法也就是返回cell的方法中实现图片的切换用的是cell的indexPath.item-1 = -1或者 +1 再加上图片自己的索引值self.index就会得出即将滚出的cell要显示的是上一张图片仍是下一张图片.
2. cell滚动结束后要计算当前cell显示图片的索引.是经过scrollview的偏移量contentoffset.x除以scrollview的宽度,计算出当前cell的图片的索引保存.以后再滑动cell的时候,会调用数据源第三个方法,就会使用保存下来的self.index以及加上cell的indexPath.item-1来计算要显示的图片的索引.
3. 注意点: 消息循环模式,代码滚动偏移触发的方法与手动拖拽触发的方法不一样.
```并发