iOS实现自动循环播放的banner实例详解

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

前言

对于banner轮播图,相信大家都会经常用到。自动循环播放的banner是很常见的UI组件。如何实现呢?下面就来给大家详细介绍下,话不多说了,下面来一起学习学习吧。

1.实现思路

1.横向滚动的banner。

  • UIScrollViw+UIImageView.
  • UICollectionView+UICollectionViewCell.
  • 前者需要自己做重用UIImageView,后者可以直接重用UICollectionViewCell。如果前者没有做重用,多占用内存。

2.自动循环播放banner。

  • 可以使用计时器触发循环播放.
  • 拖动或手动滑动banner时,停止自动循环播放banner。手势停止后,开启自动循环播放banner。

3.特殊banner位的处理。

  • 处于第1个或最后1个时,为保证横向自动滑动效果流畅性,不跳动,需要特殊处理下。
  • 在生成banner时,第1个前面插入最后1个banner。最后1个banner后面插入第1个banner。当滑动到最后1个banner时,重置于第2个banner位。

2.本文采用第二种:UICollectionView+UICollectionViewCell

关键代码实现

2.1生成banner的特殊处理

- (void)setBannerList:(NSArray<KBannerItem *> *)bannerList {
 if (bannerList.count > 1) {
  NSMutableArray *itemList = [NSMutableArray arrayWithArray:bannerList];
  [itemList insertObject:bannerList.lastObject atIndex:0];
  [itemList addObject:bannerList.firstObject];
  _bannerList = itemList;
 }else{
  _bannerList = bannerList;
 }
 if (self.bannerList.count > 1) {
  self.bannerPageControl.numberOfPages = self.bannerList.count - 2;
 }else{
  self.bannerPageControl.numberOfPages = 0;
 }
 self.noBannerImageView.hidden = self.bannerList.count > 0;
 self.bannerPageControl.currentPage = 0;
 [self.collectionView reloadData];
 self.collectionView.contentOffset = CGPointMake(CGRectGetWidth(self.collectionView.frame), 0);
}

2.2 banner自动循环播放触发的事件

- (void)handleBannerChangeEvent:(id)sender {
 if (_bannerPageControl.numberOfPages <= 1) {
  return;
 }
 NSInteger page = _bannerPageControl.currentPage + 1;
 [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:page + 1 inSection:0] atScrollPosition:UICollectionViewScrollPositionRight animated:YES];
}

2.3开启自动播放或关闭自动播放bannner。

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
 self.countTimer.isOpen = NO;
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView
     willDecelerate:(BOOL)decelerate {
 self.countTimer.isOpen = YES;
}

2.4.滑动时的特殊处理。

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
 if (_bannerList.count <=1) {
  return;
 }
 CGFloat width = CGRectGetWidth(scrollView.frame);
 NSInteger currentPage = scrollView.contentOffset.x / width;
 if (currentPage == 0) {
  if (scrollView.contentOffset.x < 0) {
   [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:_bannerList.count - 2 inSection:0] atScrollPosition:UICollectionViewScrollPositionRight animated:NO];
   self.bannerPageControl.currentPage = _bannerList.count - 2;
  }else{
   [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0] atScrollPosition:UICollectionViewScrollPositionRight animated:NO];
   self.bannerPageControl.currentPage = 0;
  }
 }else if (currentPage == _bannerList.count - 1) {
  self.bannerPageControl.currentPage = 0;
  [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0] atScrollPosition:UICollectionViewScrollPositionRight animated:NO];
 }else{
  self.bannerPageControl.currentPage = currentPage - 1;
 }
}

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。

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

iOS UISearchController的使用方法

本文主要介绍了iOS UISearchController的使用方法,具有很好的参考价值,下面跟着小编一起来看下吧
收藏 0 赞 0 分享

IOS 中 new 和 alloc init 的对比

这篇文章主要介绍了IOS 中 new 和 alloc init 的区别的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

IOS 字符串常用处理详细介绍

这篇文章主要介绍了IOS 字符串常用处理详细介绍的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

iOS开发支付宝支付成功返回字符串的处理操作

很多朋友在做ios开发项目时,遇到支付宝支付成功返回字符串的处理操作不知道该怎么办,今天小编给大家分享实例代码,需要的朋友参考下
收藏 0 赞 0 分享

IOS CoreLocation实现系统自带定位的方法

本篇文章主要介绍了IOS Core Location实现系统自带定位的方法,非常具有实用价值,需要的朋友可以参考下。
收藏 0 赞 0 分享

iOS之点击通知栏的通知进入程序的触发事件

本文主要介绍了iOS中点击通知栏的通知进入程序的触发事件的相关知识,具有很好的参考价值,下面跟着小编一起来看下吧
收藏 0 赞 0 分享

IOS  Swift基础之switch用法详解

这篇文章主要介绍了IOS Swift基础之switch用法详解的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

IOS React等Title不显示问题解决办法

这篇文章主要介绍了IOS React等Title不显示问题解决办法的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

iOS UIScrollView滚动视图/无限循环滚动/自动滚动的实例代码

这篇文章主要介绍了iOS UIScrollView滚动视图/无限循环滚动/自动滚动,需要的朋友可以参考下
收藏 0 赞 0 分享

简单实现iOS指纹解锁(TouchID)

这篇文章主要介绍了如何简单实现iOS指纹解锁,验证TouchID,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多