无限轮播(循环展示)

无限轮播(循环展示)

一、简单说明

  之前的程序还存在一个问题,那就是不能循环展示,因为plist文件中只有五个数组,因此第一个和最后一个之后就没有了,下面介绍处理这种循环展示问题的小技巧。

  

方法一:使用一个for循环,循环200次,创建200*=1000个模型,且默认程序启动后处在第100组的位置,向前有500个模型,向后也有500个模型,产生一种循环展示的假象。

  代码如下:

 8
 9 #import "YYViewController.h"
10 #import "MJExtension.h"
11 #import "YYnews.h"
12 #import "YYcell.h"
13
14 #define YYIDCell @"cell"
15
16 @interface YYViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
17 @property (weak, nonatomic) IBOutlet UICollectionView *collectinView;
18 @property(nonatomic,strong)NSMutableArray *news;
19 @end
20
21 @implementation YYViewController
22
23 #pragma mark-懒加载
24 //-(NSArray *)news
25 //{
26 //    if (_news==nil) {
27 //        _news=[YYnews objectArrayWithFilename:@"newses.plist"];
28 //    }
29 //    return _news;
30 //}
31 -(NSMutableArray *)news
32 {
33     if (_news==nil) {
34         _news=[NSMutableArray array];
35         for (int i=0; i<200; i++) {
36             NSArray *array=[YYnews objectArrayWithFilename:@"newses.plist"];
37             [_news addObjectsFromArray:array];
38         }
39     }
40     return _news;
41 }
42
43 - (void)viewDidLoad
44 {
45     [super viewDidLoad];
46     //注册cell
47 //    [self.collectinView registerClass:[YYimageCell class] forCellWithReuseIdentifier:YYCell];
48     [self.collectinView registerNib:[UINib nibWithNibName:@"YYcell" bundle:nil] forCellWithReuseIdentifier:YYIDCell];
49
50     //默认处于第0组的第500个模型的左边
51     [self.collectinView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:500 inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
52
53 }
54
55 #pragma mark- UICollectionViewDataSource
56 //一共多少组,默认为1组
57 -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
58 {
59     return 1;
60 }
61 -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
62 {
63     return self.news.count;
64 }
65
66 -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
67 {
68     YYcell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:YYIDCell forIndexPath:indexPath];
69     cell.news=self.news[indexPath.item];
70     NSLog(@"%p,%d",cell,indexPath.item);
71     return cell;
72 }
73
74 #pragma mark-UICollectionViewDelegate
75 @end

  打印查看所处的索引(全程依然只创建了两个cell):

  

说明:

  [self.collectinView scrollToItemAtIndexPath:<#(NSIndexPath *)#> atScrollPosition:<#(UICollectionViewScrollPosition)#> animated:<#(BOOL)#>]

 //默认处于第0组的第500个模型的左边

方法二:设置其有100组,那么一共有100*5=500个模型。且设置默认处于第50组的索引为0处。

  代码如下:

 8
 9 #import "YYViewController.h"
10 #import "MJExtension.h"
11 #import "YYnews.h"
12 #import "YYcell.h"
13
14 #define YYIDCell @"cell"
15
16 @interface YYViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
17 @property (weak, nonatomic) IBOutlet UICollectionView *collectinView;
18 @property(nonatomic,strong)NSArray *news;
19 @end
20
21 @implementation YYViewController
22
23 #pragma mark-懒加载
24 -(NSArray *)news
25 {
26     if (_news==nil) {
27         _news=[YYnews objectArrayWithFilename:@"newses.plist"];
28     }
29     return _news;
30 }
31 //-(NSMutableArray *)news
32 //{
33 //    if (_news==nil) {
34 //        _news=[NSMutableArray array];
35 //        for (int i=0; i<200; i++) {
36 //            NSArray *array=[YYnews objectArrayWithFilename:@"newses.plist"];
37 //            [_news addObjectsFromArray:array];
38 //        }
39 //    }
40 //    return _news;
41 //}
42
43 - (void)viewDidLoad
44 {
45     [super viewDidLoad];
46     //注册cell
47 //    [self.collectinView registerClass:[YYimageCell class] forCellWithReuseIdentifier:YYCell];
48     [self.collectinView registerNib:[UINib nibWithNibName:@"YYcell" bundle:nil] forCellWithReuseIdentifier:YYIDCell];
49
50     //默认处于第0组的第500个模型的左边
51 //    [self.collectinView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:500 inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
52
53      [self.collectinView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:50] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
54
55 }
56
57 #pragma mark- UICollectionViewDataSource
58 //一共多少组,默认为1组
59 -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
60 {
61     return 100;
62 }
63 -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
64 {
65     return self.news.count;
66 }
67
68 -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
69 {
70     YYcell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:YYIDCell forIndexPath:indexPath];
71     cell.news=self.news[indexPath.item];
72     NSLog(@"%p,%d",cell,indexPath.item);
73     return cell;
74 }
75
76 #pragma mark-UICollectionViewDelegate
77 @end

注意:上面的两种方法都创建了大量的无用的模型,不太可取。且在实际开发中,建议模型的总数不要太大,因为在其内部需要遍历计算所有控件的frame。

  如果模型数量太大,会占用资源。

改进建议:可以监听手指在上面的滚动,当停止滚动的时候,又重新设置初始的中间位置。

时间: 2024-07-29 13:05:18

无限轮播(循环展示)的相关文章

iOS开发UI篇—无限轮播(循环利用)

一.无限轮播  1.简单说明 在开发中常需要对广告或者是一些图片进行自动的轮播,也就是所谓的无限滚动. 在开发的时候,我们通常的做法是使用一个UIScrollView,在UIScrollView上面添加多个imageView,然后设置imageView的图片,和scrollView的滚动范围. 以前的做法: 一般而言,轮播的广告或者是图片数量都不会太多(3~5张).所以,并不会太多的去考虑性能问题.但是如果图片过多(比如有16张图片,就需要创建16个imageView),那么就不得不考虑性能问题

无限轮播(循环利用)

无限轮播(循环利用) 一.无限轮播  1.简单说明 在开发中常需要对广告或者是一些图片进行自动的轮播,也就是所谓的无限滚动. 在开发的时候,我们通常的做法是使用一个UIScrollView,在UIScrollView上面添加多个imageView,然后设置imageView的图片,和scrollView的滚动范围. 以前的做法: 一般而言,轮播的广告或者是图片数量都不会太多(3~5张).所以,并不会太多的去考虑性能问题.但是如果图片过多(比如有16张图片,就需要创建16个imageView),那

无限轮播(新闻数据展示)

无限轮播(新闻数据展示) 一.实现效果        二.实现步骤 1.前期准备 (1)导入数据转模型的第三方框架MJExtension (2)向项目中添加保存有“新闻”数据的plist文件 (3)导入用到的图片素材 2.步骤和代码 (1)新建一个数据模型 该模型的代码设计如下: YYnews.h文件 5 6 #import <Foundation/Foundation.h> 7 8 @interface YYnews : NSObject 9 @property(nonatomic,copy

iOS开发UI篇—无限轮播(功能完善)

iOS开发UI篇—无限轮播(功能完善) 一.自动滚动 添加并设置一个定时器,每个2.0秒,就跳转到下一条. 获取当前正在展示的位置. 1 [self addNSTimer]; 2 } 3 4 -(void)addNSTimer 5 { 6 // NSTimer timerWithTimeInterval:<#(NSTimeInterval)#> target:<#(id)#> selector:<#(SEL)#> userInfo:<#(id)#> repe

无限轮播(功能完善)

无限轮播(功能完善) 一.自动滚动 添加并设置一个定时器,每个2.0秒,就跳转到下一条. 获取当前正在展示的位置. 1 [self addNSTimer]; 2 } 3 4 -(void)addNSTimer 5 { 6 // NSTimer timerWithTimeInterval:<#(NSTimeInterval)#> target:<#(id)#> selector:<#(SEL)#> userInfo:<#(id)#> repeats:<#

iOS:实现图片的无限轮播(二)---之使用第三方库SDCycleScrollView

下载链接:github不断更新地址:https://github.com/gsdios/SDCycleScrollView 使用原理:采用UICollectionView的重用机制和循环滚动的方式实现图片的无限轮播,播放非常顺畅,解决了UISCrollView使用时从最后一张跳到第一张时的生硬状态. 主要类截图: SDCollectionViewCell:用来重用的item,即显示图片的视图: SDCycleScrollView: 对外提供的一个创建轮播器的接口类,使用者就是直接使用这个类来实现

新版无限轮播视图,2个imageView完成(不依赖任何三方库)

笔者最新改写的无限轮播视图,完全不依赖任何三方库,2个imageView实现无限轮播. 特点: 1.支持图片缓存到本地,亦支持一键清除缓存. 2.超级轻量,没有任何第三方框架参合,占用内存极小 3.高度自定义,支持修改pageControll的显示图片及显示位置 4.图片展示模式多样性,支持轮播展示与渐入渐出模式展示 5.支持自动滚动 6.创建方式多样,支持代码及storyboard创建 7.支持显示图片简介,具体见GitHub效果图 代码实现: .h // // WYScrollView.h

iOS开发之ImageView复用实现图片无限轮播

在上篇博客中iOS开发之多图片无缝滚动组件封装与使用给出了图片无限轮播的实现方案之一,下面在给出另一种解决方案.今天博客中要说的就是在ScrollView上贴两个ImageView, 把ImageView进行交替切换来实现图片的无限轮播,在轮播时去修改ImageView上的图片.上一篇博客中是有几张图片就实例化几个ImageView, 然后事先把Image贴到相应的ImageView上,这种做法比较简单,而且易于实现. 今天这篇博客就要实现使用两张ImageView, 交替的区展示Image,

无限轮播的实现,未加自动轮播效果(非原创)

最近一直在看视频,学到了一个无限轮播,今天把它给写下来保存,方便日后自己查阅. html 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <meta name="keywords" content=""> 6 <meta name="description" content="