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

简介:

一、自动滚动

添加并设置一个定时器,每个2.0秒,就跳转到下一条。

  获取当前正在展示的位置。

复制代码
 1  [self addNSTimer];
 2 }
 3  4 -(void)addNSTimer
 5 {
 6 // NSTimer timerWithTimeInterval:<#(NSTimeInterval)#> target:<#(id)#> selector:<#(SEL)#> userInfo:<#(id)#> repeats:<#(BOOL)#>  7 [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
 8 }
 9 -(void)nextPage
10 {
11 NSLog(@"%@",[self.collectinView indexPathsForVisibleItems]);
12 // NSIndexPath *currentIndexPath=[[self.collectinView indexPathsForVisibleItems]lastObject]; 13 }
复制代码

  打印查看:

实现步骤:

(1)添加并设置定时器

(2)设置定时器的调用方法

  1)获取当前正在展示的位置

  2)计算出下一个需要展示的位置

  3)通过动画滚动到下一个位置

  注意点:需要进行判断。

实现代码:

复制代码
 1 - (void)viewDidLoad
 2 {
 3  [super viewDidLoad];
 4 //注册cell
 5 // [self.collectinView registerClass:[YYimageCell class] forCellWithReuseIdentifier:YYCell];  6 [self.collectinView registerNib:[UINib nibWithNibName:@"YYcell" bundle:nil] forCellWithReuseIdentifier:YYIDCell];
 7  8  9 [self.collectinView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:50] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
10  [self addNSTimer];
11 }
12 13 -(void)addNSTimer
14 {
15 // NSTimer timerWithTimeInterval:<#(NSTimeInterval)#> target:<#(id)#> selector:<#(SEL)#> userInfo:<#(id)#> repeats:<#(BOOL)#> 16 17 NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
18 //添加到runloop中 19  [[NSRunLoop mainRunLoop]addTimer:timer forMode:NSRunLoopCommonModes];
20 }
21 -(void)nextPage
22 {
23 // NSLog(@"%@",[self.collectinView indexPathsForVisibleItems]);
24 //1)获取当前正在展示的位置 25 NSIndexPath *currentIndexPath=[[self.collectinView indexPathsForVisibleItems]lastObject];
26 27 //2)计算出下一个需要展示的位置 28 NSInteger nextItem=currentIndexPath.item+1;
29 NSInteger nextSection=currentIndexPath.section;
30 if (nextItem==self.news.count) {
31 nextItem=0;
32 nextSection++;
33  }
34 NSIndexPath *nextIndexPath=[NSIndexPath indexPathForItem:nextItem inSection:nextSection];
35 36 //3)通过动画滚动到下一个位置 37  [self.collectinView scrollToItemAtIndexPath:nextIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
38 }
复制代码

定时器的说明:

  当用户在处理其他事情的时候,定时器会停止工作。应该把定时器添加到runloop中,告诉系统在处理其他事情的时候分一部分空间给它。

二、设置页码

  在storyboard中添加一个页码控件。

实现代码:  

复制代码
 1 #pragma mark-懒加载
 2 -(NSArray *)news
 3 {
 4 if (_news==nil) {
 5 _news=[YYnews objectArrayWithFilename:@"newses.plist"];
 6 self.pageControl.numberOfPages=self.news.count;
 7  }
 8 return _news;
 9 }
10 11 - (void)viewDidLoad
12 {
13  [super viewDidLoad];
14 //注册cell
15 // [self.collectinView registerClass:[YYimageCell class] forCellWithReuseIdentifier:YYCell]; 16 [self.collectinView registerNib:[UINib nibWithNibName:@"YYcell" bundle:nil] forCellWithReuseIdentifier:YYIDCell];
17 18 19 [self.collectinView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:50] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
20  [self addNSTimer];
21 }
22 23 -(void)addNSTimer
24 {
25 // NSTimer timerWithTimeInterval:<#(NSTimeInterval)#> target:<#(id)#> selector:<#(SEL)#> userInfo:<#(id)#> repeats:<#(BOOL)#> 26 27 NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
28 //添加到runloop中 29  [[NSRunLoop mainRunLoop]addTimer:timer forMode:NSRunLoopCommonModes];
30 }
31 -(void)nextPage
32 {
33 // NSLog(@"%@",[self.collectinView indexPathsForVisibleItems]);
34 //1)获取当前正在展示的位置 35 NSIndexPath *currentIndexPath=[[self.collectinView indexPathsForVisibleItems]lastObject];
36 37 //2)计算出下一个需要展示的位置 38 NSInteger nextItem=currentIndexPath.item+1;
39 NSInteger nextSection=currentIndexPath.section;
40 if (nextItem==self.news.count) {
41 nextItem=0;
42 nextSection++;
43  }
44 NSIndexPath *nextIndexPath=[NSIndexPath indexPathForItem:nextItem inSection:nextSection];
45 46 //3)通过动画滚动到下一个位置 47  [self.collectinView scrollToItemAtIndexPath:nextIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
48 49 //4)设置页码 50  self.pageControl.currentPage=nextItem;
51 }
复制代码

 自动滚动,页码的实现效果:

三、完善

说明:监听collectionView的滚动,当手动触摸collectionView,尝试拖拽的时候,把定时器停掉,当手指移开的时候,重启定时器。

完整的实现代码:

复制代码
 1 //  2 // YYViewController.m
 3 // 07-无限滚动(循环利用)
 4 //  5 // Created by apple on 14-8-3.
 6 // Copyright (c) 2014年 yangyong. All rights reserved.
 7 //
 8  9 #import "YYViewController.h"  10 #import "MJExtension.h"  11 #import "YYnews.h"  12 #import "YYcell.h"  13  14 #define YYIDCell @"cell"
 15 //注意:这里需要考虑用户的手动拖拽  16 #define YYMaxSections 100
 17 @interface YYViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
 18 @property (weak, nonatomic) IBOutlet UICollectionView *collectinView;
 19 @property(nonatomic,strong)NSArray *news;
 20 @property (weak, nonatomic) IBOutlet UIPageControl *pageControl;
 21 @property(nonatomic,strong)NSTimer *timer;
 22  23 @end  24  25 @implementation YYViewController
 26  27 #pragma mark-懒加载
 28 -(NSArray *)news
 29 {
 30 if (_news==nil) {
 31 _news=[YYnews objectArrayWithFilename:@"newses.plist"];
 32 self.pageControl.numberOfPages=self.news.count;
 33  }
 34 return _news;
 35 }
 36  37 - (void)viewDidLoad
 38 {
 39  [super viewDidLoad];
 40 //注册cell
 41 // [self.collectinView registerClass:[YYimageCell class] forCellWithReuseIdentifier:YYCell];  42 [self.collectinView registerNib:[UINib nibWithNibName:@"YYcell" bundle:nil] forCellWithReuseIdentifier:YYIDCell];
 43  44  45 [self.collectinView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:50] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
 46  [self addNSTimer];
 47 }
 48  49 //添加定时器  50 -(void)addNSTimer
 51 {
 52 NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
 53 //添加到runloop中  54  [[NSRunLoop mainRunLoop]addTimer:timer forMode:NSRunLoopCommonModes];
 55 self.timer=timer;
 56 }
 57  58 //删除定时器  59 -(void)removeNSTimer
 60 {
 61  [self.timer invalidate];
 62 self.timer=nil;
 63 }
 64  65 //自动滚动  66 -(void)nextPage
 67 {
 68 //1获取当前正在展示的位置  69 NSIndexPath *currentIndexPath=[[self.collectinView indexPathsForVisibleItems]lastObject];
 70  71 //马上显示回最中间那组的数据  72 NSIndexPath *currentIndexPathRest=[NSIndexPath indexPathForItem:currentIndexPath.item inSection:YYMaxSections/2];
 73  [self.collectinView scrollToItemAtIndexPath:currentIndexPathRest atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
 74  75 //2计算出下一个需要展示的位置  76 NSInteger nextItem=currentIndexPathRest.item+1;
 77 NSInteger nextSection=currentIndexPathRest.section;
 78 if (nextItem==self.news.count) {
 79 nextItem=0;
 80 nextSection++;
 81  }
 82 NSIndexPath *nextIndexPath=[NSIndexPath indexPathForItem:nextItem inSection:nextSection];
 83  84 //3通过动画滚动到下一个位置  85  [self.collectinView scrollToItemAtIndexPath:nextIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
 86  87 // //4)设置页码
 88 // self.pageControl.currentPage=nextItem;  89 }
 90  91 #pragma mark- UICollectionViewDataSource
 92 //一共多少组,默认为1组  93 -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
 94 {
 95 return YYMaxSections;
 96 }
 97 -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
 98 {
 99 return self.news.count;
100 }
101 102 -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
103 {
104 YYcell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:YYIDCell forIndexPath:indexPath];
105 cell.news=self.news[indexPath.item];
106 NSLog(@"%p,%d",cell,indexPath.item);
107 return cell;
108 }
109 110 #pragma mark-UICollectionViewDelegate
111 //当用户开始拖拽的时候就调用 112 -(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
113 {
114  [self removeNSTimer];
115 }
116 //当用户停止拖拽的时候调用 117 -(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
118 {
119  [self addNSTimer];
120 }
121 //设置页码 122 -(void)scrollViewDidScroll:(UIScrollView *)scrollView
123 {
124 int page=(int)(scrollView.contentOffset.x/scrollView.frame.size.width+0.5)%self.news.count;
125 self.pageControl.currentPage=page;
126 }
127 @end
复制代码

补充说明:

  实现瀑布流最理想的做法是继承UIScrollView,不建议使用多个UITableView的方式实现(这种方式无法实现cell的循环利用,且禁止了cell的滚动时间后,整体的tableView需要随着滚动会出现空白)。

  也可以使用collectionView来实现,但使用这种方法需要自定义collectionView的布局(流水布局)

目录
相关文章
|
2天前
|
移动开发 开发工具 Android开发
探索安卓与iOS开发的差异:技术选择的影响
【8月更文挑战第17天】 在移动应用开发的广阔天地中,安卓和iOS两大平台各领风骚。本文通过比较这两个平台的编程语言、开发工具及市场策略,揭示了技术选择对开发者和产品成功的重要性。我们将从开发者的视角出发,深入探讨不同平台的技术特性及其对项目实施的具体影响,旨在为即将步入移动开发领域的新手提供一个清晰的指南,同时给予资深开发者新的思考角度。
|
3天前
|
vr&ar Android开发 iOS开发
探索安卓和iOS开发的未来趋势
在移动应用开发的广阔天地里,安卓和iOS两大平台如同双子星座般璀璨夺目。随着技术的不断进步,这两个平台的开发趋势也在悄然发生着变化。本文将带你一探究竟,看看未来安卓和iOS开发将会迎来哪些令人激动的新特性和挑战。让我们一起跟随技术的脚步,开启这场探索之旅吧!
|
4天前
|
移动开发 Java Android开发
安卓与iOS开发:异同探析
在移动应用开发的广阔天地中,安卓和iOS两大平台各自占据半壁江山。本文旨在深入探讨这两个平台在开发环境、编程语言、用户界面设计、性能优化及市场分布等方面的异同,为开发者提供实用的比较视角和决策参考。通过对比分析,我们不仅能更清晰地认识到各平台的特性,还能洞察未来移动开发的可能趋势。
|
5天前
|
Java 开发工具 Android开发
探索Android和iOS开发的差异与挑战
在移动应用开发的广阔天地中,Android和iOS两大平台如同两座高峰,各自拥有独特的风景。本文将深入探讨这两个平台的开发差异,包括编程语言、开发工具、用户界面设计等方面,并分析开发者面临的挑战。无论你是初涉移动应用开发的新手,还是已经在这条路上走了很远的老手,这篇文章都将为你提供新的视角和思考。让我们一起走进这个充满创新与挑战的世界,发现那些隐藏在代码背后的秘密。
|
3天前
|
Java Android开发 iOS开发
探索安卓与iOS开发的差异:从新手到专家的旅程
本文将带你走进移动应用开发的两大平台——安卓和iOS,揭示它们之间的主要差异。我们将从新手的视角出发,逐步深入到更复杂的技术层面,帮助你理解这两个平台的开发环境、编程语言和用户界面设计等方面的不同。无论你是刚入门的新手,还是有一定经验的开发者,这篇文章都将为你提供有价值的见解和建议。现在,让我们一起开始这段探索之旅吧!
|
3天前
|
搜索推荐 Android开发 iOS开发
探索安卓与iOS开发的差异之美
在数字时代的浪潮中,移动应用开发如同一场精心编排的交响乐,安卓和iOS这两大平台扮演着不同乐器的角色,各自以独特的方式奏响。本文将带领读者走进这场音乐盛宴,感受两大平台在开发过程中所展现的不同韵律,从设计理念到用户体验,从市场占有率到生态系统,我们将一探究竟,欣赏它们如何在竞争激烈的市场中和谐共存,共同推动技术的进步与创新。
12 0
|
4天前
|
开发工具 Android开发 iOS开发
探索安卓与iOS开发的差异:构建未来应用的关键考量
在数字时代,选择正确的开发平台是成功的一半。本文深入探讨了安卓与iOS两大移动操作系统的开发差异,并分析了各自对创新、用户体验和市场需求的响应。通过比较两者的设计哲学、开发工具、市场覆盖和用户参与度,我们揭示了每个平台的独特优势和潜在挑战,旨在为开发者提供决策时的洞见,帮助他们在竞争激烈的应用市场中做出明智的选择。