IOS使用UICollectionView实现无限轮播效果

所属分类: 软件编程 / IOS 阅读数: 1880
收藏 0 赞 0 分享

一、案例演示

本案例Demo演示的是一个首页轮播的案例,支持手动轮播和自动轮播。知识点主要集中在UICollectionView和NSTimer的使用。

二、知识储备

2.1、UICollectionView横向布局

只需要设置UICollectionViewFlowLayout的scrollDirection为UICollectionViewScrollDirectionHorizontal即可。

2.2、NSTimer的基本使用

NSTimer的初始化:

复制代码 代码如下:
 + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(nullable id)userInfo repeats:(BOOL)yesOrNo;

1)、(NSTimeInterval)ti : 预订一个Timer,设置一个时间间隔。
表示输入一个时间间隔对象,以秒为单位,一个>0的浮点类型的值,如果该值<0,系统会默认为0.1。
2)、target:(id)aTarget : 表示发送的对象,如self
3)、selector:(SEL)aSelector : 方法选择器,在时间间隔内,选择调用一个实例方法
4)、userInfo:(nullable id)userInfo : 需要传参,可以为nil
5)、repeats:(BOOL)yesOrNo : 当YES时,定时器会不断循环直至失效或被释放,当NO时,定时器会循环发送一次就失效。

开启定时器:

复制代码 代码如下:
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

关闭定时器:

[self.timer invalidate];

2.3、自动轮播和手动轮播的切换

初始化的时候,我们默认开启定时器,定时执行切换到下一张图片的函数。当用户触摸到View的时候,我们则要关闭定时器,手动的进行UICollectionView的切换。当用户的手离开了View,我们要重新打开定时器,进行自动轮播的切换。

三、关键代码分析

3.1、生成UICollectionViewFlowLayout对象,设置他的滚动方向为水平滚动  

 UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
 flowLayout.itemSize = CGSizeMake(SCREEN_WIDTH, 200);
 flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
 flowLayout.minimumLineSpacing = 0;

3.2、初始化UICollectionView对象 

 UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, self.navBarHeight, SCREEN_WIDTH, 200) collectionViewLayout:flowLayout];
 collectionView.delegate = self;
 collectionView.dataSource = self;
 collectionView.showsHorizontalScrollIndicator = NO;
 collectionView.pagingEnabled = YES;
 collectionView.backgroundColor = [UIColor clearColor];
 [self.view addSubview:collectionView];

3.3、UICollectionView的UICollectionViewDataSource代理方法

#pragma mark- UICollectionViewDataSource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
 return YYMaxSections;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
 return self.newses.count;
}

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


 YYCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:YYIDCell forIndexPath:indexPath];
 if(!cell){
 cell = [[YYCell alloc] init];
 }
 cell.news=self.newses[indexPath.item];
 return cell;
}

3.4、定时器的开启和关闭

#pragma mark 添加定时器
-(void) addTimer{
 NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(nextpage) userInfo:nil repeats:YES];
 [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
 self.timer = timer ;

}

#pragma mark 删除定时器
-(void) removeTimer{
 [self.timer invalidate];
 self.timer = nil;
}

3.5、手动切换 和 自动轮播 的切换

-(void) scrollViewWillBeginDragging:(UIScrollView *)scrollView{
 [self removeTimer];
}

#pragma mark 当用户停止的时候调用
-(void) scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
 [self addTimer];

}

#pragma mark 设置页码
-(void) scrollViewDidScroll:(UIScrollView *)scrollView{
 int page = (int) (scrollView.contentOffset.x/scrollView.frame.size.width+0.5)%self.newses.count;
 self.pageControl.currentPage =page;
}

3.6、自动轮播切换到下一个View的方法

-(void) nextpage{
 NSIndexPath *currentIndexPath = [[self.collectionView indexPathsForVisibleItems] lastObject];

 NSIndexPath *currentIndexPathReset = [NSIndexPath indexPathForItem:currentIndexPath.item inSection:YYMaxSections/2];
 [self.collectionView scrollToItemAtIndexPath:currentIndexPathReset atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];

 NSInteger nextItem = currentIndexPathReset.item +1;
 NSInteger nextSection = currentIndexPathReset.section;
 if (nextItem==self.newses.count) {
 nextItem=0;
 nextSection++;
 }
 NSIndexPath *nextIndexPath = [NSIndexPath indexPathForItem:nextItem inSection:nextSection];

 [self.collectionView scrollToItemAtIndexPath:nextIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
}

Demo下载地址:https://github.com/yixiangboy/YXCollectionView

以上就是本文的全部内容,希望对大家的学习有所帮助。

更多精彩内容其他人还在看

iOS App中UILabel的自定义及在Auto Layout中的使用

这篇文章主要介绍了iOS App中UILabel的自定义及在Auto Layout中的使用,示例代码为传统的Objective-C语言,需要的朋友可以参考下
收藏 0 赞 0 分享

iOS应用开发中UITableView的分割线的一些设置技巧

这篇文章主要介绍了iOS应用开发中UITableView分割线的一些设置技巧,包括消除分割线的方法,示例代码为传统的Objective-C语言,需要的朋友可以参考下
收藏 0 赞 0 分享

详解iOS时间选择框

这篇文章主要为大家详细介绍了详解iOS时间选择框,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

详解iOS App开发中UIViewController的loadView方法使用

这篇文章主要介绍了详解iOS App开发中UIViewController的loadView方法使用,讲解了访问view属性时loadView方法的调用及使用loadView时的一些注意点,需要的朋友可以参考下
收藏 0 赞 0 分享

详解在iOS App中自定义和隐藏状态栏的方法

这篇文章主要介绍了在iOS App中自定义和隐藏状态栏的方法,在顶部时某些状况下即用应用内的状态栏覆盖系统本身的,代码示例为Objective-C语言,需要的朋友可以参考下
收藏 0 赞 0 分享

iOS开发实现音频播放功能

本文给大家分享的是在IOS开发过程中实现音频播放的功能,讲解的十分细致,有需要的小伙伴可以参考下
收藏 0 赞 0 分享

IOS开发实现录音功能

本文给大家分享的是一个IOS开发中实现录音功能的实例,并简单给大家解析一下,有需要的小伙伴可以参考下
收藏 0 赞 0 分享

iOS实现图片轮播效果

这篇文章主要为大家详细介绍了IOS实现图片轮播效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

iOS通过http post上传图片

这篇文章主要介绍了iOS通过http post上传图片的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

IOS框架Spring常用的动画效果

本文给大家介绍的是在IOS开发中常用的动画效果以及自定义转场动画特效的代码,非常的简单实用,有需要的小伙伴可以参考下
收藏 0 赞 0 分享
查看更多