您的位置:首页 > 移动开发 > IOS开发

iOS -- 用collectionView实现图片的无限轮播

2016-09-24 17:59 323 查看
用collectionView来实现轮播的用途不仅只是图片还可以用在文字广告热点的轮播。当然还有其他的方法

无线轮播最重要的当然就是要实现最后一张和第一张的无缝切换。

网上的demo大多都用了很多组带着侥幸心理认为用户不会有耐心滑到最后一组

我用了两组来实现轮播。



其核心在于:

/**当用户即将开始拖拽的时候就调用*/
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
[self removeTimer];

// 当前正在展示的位置
NSIndexPath *currentIndexPath = [[self.adsSrollView indexPathsForVisibleItems] lastObject];

//当处于第一组的第一个item的时候  ——> 滑到第二组的第一个item
if (scrollView.contentOffset.x / scrollView.bounds.size.width == 1) {

// 悄悄滑到第二组的第一个位置
NSIndexPath *currentIndexPathReset = [NSIndexPath indexPathForItem:currentIndexPath.item inSection:maxSection/2];
[self.adsSrollView scrollToItemAtIndexPath:currentIndexPathReset atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
}

//当处于第二组的最后一个item的时候 ——> 滑到第一组的最后一个item
if (scrollView.contentOffset.x / scrollView.bounds.size.width == self.dataArray.count*2 -1) {
// 悄悄滑到第-组的最后一个位置
NSIndexPath *currentIndexPathReset = [NSIndexPath indexPathForItem:currentIndexPath.item inSection:0];
[self.adsSrollView scrollToItemAtIndexPath:currentIndexPathReset atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  无限轮播