美文网首页
iOS-可手动翻页/自动广播跑马灯 (广播)

iOS-可手动翻页/自动广播跑马灯 (广播)

作者: 麦穗0615 | 来源:发表于2019-03-20 16:27 被阅读0次

前言:
前期看过很多跑马灯觉得都挺好的,用的最多的是第三方TXScrollLabelView,可轮播文字,也可点击跳转相应的页面。但是就是不能手动的翻页,我也搜过,百度过,查到了SDCycleScrollView(轮播图一般用这个),它可以改成手动翻页的但是巨麻烦,还得改里面的源码。回头想了想,轮播轮播,广播也相当于是一个轮播图,只是体现形式变了。又查看了很多资料,一直有这个想法。也不知道弄的怎么样,就拿collectionView封装了一个可翻页/可点击/可自动轮播的轮播图。

示例

核心代码
我认为最核心代码就下面这个 (定时器执行的方法/分组 分了20组)

 - (void)beginUpdateUI {

 // 1、当前正在展示的位置
 NSIndexPath *currentIndexPath = [[self.collectionView indexPathsForVisibleItems] lastObject];
 // 马上显示回最中间那组的数据 (与当前展示的位置一样Row 组数不同)
NSIndexPath *resetCurrentIndexPath = [NSIndexPath indexPathForItem:currentIndexPath.item inSection:0.5 * scrollMaxSections];
[self.collectionView scrollToItemAtIndexPath:resetCurrentIndexPath atScrollPosition:UICollectionViewScrollPositionBottom animated:NO];

  // 2、计算出下一个需要展示的位置
 NSInteger nextItem = resetCurrentIndexPath.item + 1;
 NSInteger nextSection = resetCurrentIndexPath.section;
// 如果是当前组最后一行把下一行的item序号置于0 (第一个)
 if (nextItem == self.muBroadcastData.count) {
     nextItem = 0;
     nextSection++;
  }

 NSIndexPath *nextIndexPath = [NSIndexPath indexPathForItem:nextItem inSection:nextSection];
 //    NSLog(@"next:%lu", nextIndexPath.row);
 // 3、通过动画滚动到下一个位置
  [self.collectionView scrollToItemAtIndexPath:nextIndexPath atScrollPosition:UICollectionViewScrollPositionBottom animated:YES];
   }
 。。。

【备注:】在这里留下点痕迹,有需求的可以下载,有意见的可以提出。
方便以后项目有类似的需求。
点我下载👍

相关文章

网友评论

      本文标题:iOS-可手动翻页/自动广播跑马灯 (广播)

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