美文网首页
iOS无限轮播器

iOS无限轮播器

作者: block_smile | 来源:发表于2017-04-16 09:42 被阅读93次

地址:github.com/junjiewang1111/unlimitedUIImageView
首先,创建轮播器的视图:

JJCycleView*cycleView = [[JJCycleView alloc] initWithFrame:CGRectMake(0, 20,self.view.bounds.size.width, 200)];

本地图片资源获取:

NSMutableArray*imageURLs = [NSMutableArray array];
for(inti = 0; i<5 ; i++) {
  NSString*imageName = [NSString     stringWithFormat:@"Home_Scroll_%02d.jpg",i+1];
  NSURL*url = [[NSBundle mainBundle]URLForResource:imageName withExtension:nil];
  [imageURLs addObject:url];
  }
self.cycleView.imageURLs= imageURLs;

JJCycleView使用UICollectionView,实现数据源方法,完成数据的传递都是比较简单的.轮播器的播放,需要计算contentOffset.x,根据偏移量计算是第几页.

- (void)scrollViewDidScroll:(UIScrollView*)scrollView{
//获取偏移量
  CGFloatoffsetX = scrollView.contentOffset.x;
//计算页数
  NSIntegerpage = (offsetX +self.bounds.size.width* .5)/self.bounds.size.width;
  self.pageControl.currentPage= page %self.imageURLs.count;
  }

轮播器的滚动分自动和手动,手动时需要暂停NSTimer计时器,当手动拖动完成时,就需要重新开启,当然,计时器需要加入到RunLoop

- (void)scrollViewWillBeginDragging:(UIScrollView*)scrollView{
  self.timer.fireDate= [NSDate distantFuture];
}
//计时器开启
- (void)scrollViewDidEndDragging:(UIScrollView*)scrollView willDecelerate:(BOOL)decelerate{
  self.timer.fireDate= [NSDate dateWithTimeIntervalSinceNow:1.5];
}
  [[NSRunLoop currentRunLoop]addTimer:timer forMode:NSRunLoopCommonModes];

不是手动拖拽就调用下面方法实现自动播放

//手动滑动完成时调用
- (void)scrollViewDidEndDecelerating:(UIScrollView*)scrollView{
  //获取偏移量
  CGFloatoffsetX = scrollView.contentOffset.x;
  //计算页数
  NSIntegerpage = offsetX /self.bounds.size.width;
  NSIntegercellCount = [self.collectionView numberOfItemsInSection:0];
if(page == 0) {
  self.collectionView.contentOffset=CGPointMake(offsetX +self.imageURLs.count*self.bounds.size.width*kseed, 0);
  }elseif(page == cellCount - 1){
  self.collectionView.contentOffset=CGPointMake(offsetX -self.imageURLs.count*self.bounds.size.width*kseed, 0);}
}

一个小demo,谢谢!

相关文章

  • iOS无限轮播器

    最近自己动手造轮子,实现了一个无限图片轮播器,并扩展了丰富的自定义属性,可以自定义滚动方向、播放间隔、点击事件等。...

  • iOS无限轮播器

    好吧,我觉得还是直接给个 demo 实在点 。下载地址 轮播实现的过程大致为创建一个 scrollView ,在上...

  • iOS无限轮播器

    地址:github.com/junjiewang1111/unlimitedUIImageView首先,创建轮播器...

  • Swift无限循环的图片轮播

    前言 我们常见的一些广告位、图片轮播都是可以无限轮播的,以前参考文章 iOS开发系列--无限循环的图片浏览器,自己...

  • 无限循环图片、文字轮播器 SDCycleScrollView

    无限循环图片、文字轮播器。

  • 无限图片轮播器 --- Objective-C

    KNBannerView 无限循环轮播器:本地图片,网络图片(图片缓存) 一.功能描述及要点 1.无限图片轮播器,...

  • ios无限 自动 图片轮播器

    首先实现思路:整个collectionView中只有2个cell.中间始终显示第二个cell. 滚动:向前滚动当前...

  • 无限轮播器

    调用 分页显示

  • iOS,图片轮播器 SwpBanner

    iOS,图片轮播器 SwpBanner SwpBanner 图片轮播器, 在 App 开发中常用的一组控件, 苹果...

  • iOS --- 无限轮播

    iOS --- 无限轮播是一个常用的功能了,前段时间写项目时用的了别人写的第三方,突然发现当图片数量小于3个时会出...

网友评论

      本文标题:iOS无限轮播器

      本文链接:https://www.haomeiwen.com/subject/gdfaattx.html