码迷,mamicode.com
首页 > 其他好文 > 详细

图片无限轮播-最简单的实现方法

时间:2015-07-24 14:26:35      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:图片无限轮播

collectionView中只有三个cell 每次显示的都是第二个cell


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    

    CycleViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];

    //indexPath.item - 1 相当于index左移加右移减一

   //indexPath.item - 1 如果左移就相当于要显示第三个cell 2-1  ,相当于self.currentIndex + 1

   //indexPath.item - 1 如果右移就相当于要显示第一个cell 0-1  ,相当于self.currentIndex - 1

    NSInteger index = (self.currentIndex + indexPath.item - 1 + self.imageURLs.count) % self.imageURLs.count;

    

    cell.imageURL = self.imageURLs[index];

    

    return cell;

}


// 在滚动视图完全停止滚动后会调用的方法

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

    

    // 1. 根据contentOffset可以判断出停留住的页面

    int page = scrollView.contentOffset.x / scrollView.bounds.size.width;

    NSLog(@" %d ", page);

    

    // 2. 如果是第0页,self.currentIndex - 1,如果是第2,self.currentIndex +1;

    self.currentIndex = (self.currentIndex + page - 1 + self.imageURLs.count) % self.imageURLs.count;

    

    // 3. collection滚动会第一个页面

    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:1 inSection:0];

    [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];

}

版权声明:本文为博主原创文章,未经博主允许不得转载。

图片无限轮播-最简单的实现方法

标签:图片无限轮播

原文地址:http://blog.csdn.net/u010438187/article/details/47039519

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!