iOS开发UI篇—无限轮播(功能完善)
iOS开发UI篇—无限轮播(功能完善)
一、自动滚动
添加并设置一个定时器,每个2.0秒,就跳转到下一条。
获取当前正在展示的位置。
[self addNSTimer];
} -(void)addNSTimer
{
// NSTimer timerWithTimeInterval:<#(NSTimeInterval)#> target:<#(id)#> selector:<#(SEL)#> userInfo:<#(id)#> repeats:<#(BOOL)#>
[NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
}
-(void)nextPage
{
NSLog(@"%@",[self.collectinView indexPathsForVisibleItems]);
// NSIndexPath *currentIndexPath=[[self.collectinView indexPathsForVisibleItems]lastObject];
}
打印查看:

实现步骤:
(1)添加并设置定时器
(2)设置定时器的调用方法
1)获取当前正在展示的位置
2)计算出下一个需要展示的位置
3)通过动画滚动到下一个位置
注意点:需要进行判断。
实现代码:
- (void)viewDidLoad
{
[super viewDidLoad];
//注册cell
// [self.collectinView registerClass:[YYimageCell class] forCellWithReuseIdentifier:YYCell];
[self.collectinView registerNib:[UINib nibWithNibName:@"YYcell" bundle:nil] forCellWithReuseIdentifier:YYIDCell]; [self.collectinView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem: inSection:] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
[self addNSTimer];
} -(void)addNSTimer
{
// NSTimer timerWithTimeInterval:<#(NSTimeInterval)#> target:<#(id)#> selector:<#(SEL)#> userInfo:<#(id)#> repeats:<#(BOOL)#> NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
//添加到runloop中
[[NSRunLoop mainRunLoop]addTimer:timer forMode:NSRunLoopCommonModes];
}
-(void)nextPage
{
// NSLog(@"%@",[self.collectinView indexPathsForVisibleItems]);
//1)获取当前正在展示的位置
NSIndexPath *currentIndexPath=[[self.collectinView indexPathsForVisibleItems]lastObject]; //2)计算出下一个需要展示的位置
NSInteger nextItem=currentIndexPath.item+;
NSInteger nextSection=currentIndexPath.section;
if (nextItem==self.news.count) {
nextItem=;
nextSection++;
}
NSIndexPath *nextIndexPath=[NSIndexPath indexPathForItem:nextItem inSection:nextSection]; //3)通过动画滚动到下一个位置
[self.collectinView scrollToItemAtIndexPath:nextIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
}
定时器的说明:
当用户在处理其他事情的时候,定时器会停止工作。应该把定时器添加到runloop中,告诉系统在处理其他事情的时候分一部分空间给它。
二、设置页码
在storyboard中添加一个页码控件。
实现代码:
#pragma mark-懒加载
-(NSArray *)news
{
if (_news==nil) {
_news=[YYnews objectArrayWithFilename:@"newses.plist"];
self.pageControl.numberOfPages=self.news.count;
}
return _news;
} - (void)viewDidLoad
{
[super viewDidLoad];
//注册cell
// [self.collectinView registerClass:[YYimageCell class] forCellWithReuseIdentifier:YYCell];
[self.collectinView registerNib:[UINib nibWithNibName:@"YYcell" bundle:nil] forCellWithReuseIdentifier:YYIDCell]; [self.collectinView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem: inSection:] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
[self addNSTimer];
} -(void)addNSTimer
{
// NSTimer timerWithTimeInterval:<#(NSTimeInterval)#> target:<#(id)#> selector:<#(SEL)#> userInfo:<#(id)#> repeats:<#(BOOL)#> NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
//添加到runloop中
[[NSRunLoop mainRunLoop]addTimer:timer forMode:NSRunLoopCommonModes];
}
-(void)nextPage
{
// NSLog(@"%@",[self.collectinView indexPathsForVisibleItems]);
//1)获取当前正在展示的位置
NSIndexPath *currentIndexPath=[[self.collectinView indexPathsForVisibleItems]lastObject]; //2)计算出下一个需要展示的位置
NSInteger nextItem=currentIndexPath.item+;
NSInteger nextSection=currentIndexPath.section;
if (nextItem==self.news.count) {
nextItem=;
nextSection++;
}
NSIndexPath *nextIndexPath=[NSIndexPath indexPathForItem:nextItem inSection:nextSection]; //3)通过动画滚动到下一个位置
[self.collectinView scrollToItemAtIndexPath:nextIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES]; //4)设置页码
self.pageControl.currentPage=nextItem;
}
自动滚动,页码的实现效果:

三、完善
说明:监听collectionView的滚动,当手动触摸collectionView,尝试拖拽的时候,把定时器停掉,当手指移开的时候,重启定时器。
完整的实现代码:
//
// YYViewController.m
// 07-无限滚动(循环利用)
//
// Created by apple on 14-8-3.
// Copyright (c) 2014年 yangyong. All rights reserved.
// #import "YYViewController.h"
#import "MJExtension.h"
#import "YYnews.h"
#import "YYcell.h" #define YYIDCell @"cell"
//注意:这里需要考虑用户的手动拖拽
#define YYMaxSections 100
@interface YYViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *collectinView;
@property(nonatomic,strong)NSArray *news;
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;
@property(nonatomic,strong)NSTimer *timer; @end @implementation YYViewController #pragma mark-懒加载
-(NSArray *)news
{
if (_news==nil) {
_news=[YYnews objectArrayWithFilename:@"newses.plist"];
self.pageControl.numberOfPages=self.news.count;
}
return _news;
} - (void)viewDidLoad
{
[super viewDidLoad];
//注册cell
// [self.collectinView registerClass:[YYimageCell class] forCellWithReuseIdentifier:YYCell];
[self.collectinView registerNib:[UINib nibWithNibName:@"YYcell" bundle:nil] forCellWithReuseIdentifier:YYIDCell]; [self.collectinView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem: inSection:] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
[self addNSTimer];
} //添加定时器
-(void)addNSTimer
{
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
//添加到runloop中
[[NSRunLoop mainRunLoop]addTimer:timer forMode:NSRunLoopCommonModes];
self.timer=timer;
} //删除定时器
-(void)removeNSTimer
{
[self.timer invalidate];
self.timer=nil;
} //自动滚动
-(void)nextPage
{
//1获取当前正在展示的位置
NSIndexPath *currentIndexPath=[[self.collectinView indexPathsForVisibleItems]lastObject]; //马上显示回最中间那组的数据
NSIndexPath *currentIndexPathRest=[NSIndexPath indexPathForItem:currentIndexPath.item inSection:YYMaxSections/];
[self.collectinView scrollToItemAtIndexPath:currentIndexPathRest atScrollPosition:UICollectionViewScrollPositionLeft animated:NO]; //2计算出下一个需要展示的位置
NSInteger nextItem=currentIndexPathRest.item+;
NSInteger nextSection=currentIndexPathRest.section;
if (nextItem==self.news.count) {
nextItem=;
nextSection++;
}
NSIndexPath *nextIndexPath=[NSIndexPath indexPathForItem:nextItem inSection:nextSection]; //3通过动画滚动到下一个位置
[self.collectinView scrollToItemAtIndexPath:nextIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES]; // //4)设置页码
// self.pageControl.currentPage=nextItem;
} #pragma mark- UICollectionViewDataSource
//一共多少组,默认为1组
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return YYMaxSections;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return self.news.count;
} -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
YYcell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:YYIDCell forIndexPath:indexPath];
cell.news=self.news[indexPath.item];
NSLog(@"%p,%d",cell,indexPath.item);
return cell;
} #pragma mark-UICollectionViewDelegate
//当用户开始拖拽的时候就调用
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
[self removeNSTimer];
}
//当用户停止拖拽的时候调用
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
[self addNSTimer];
}
//设置页码
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
int page=(int)(scrollView.contentOffset.x/scrollView.frame.size.width+0.5)%self.news.count;
self.pageControl.currentPage=page;
}
@end
补充说明:
实现瀑布流最理想的做法是继承UIScrollView,不建议使用多个UITableView的方式实现(这种方式无法实现cell的循环利用,且禁止了cell的滚动时间后,整体的tableView需要随着滚动会出现空白)。
也可以使用collectionView来实现,但使用这种方法需要自定义collectionView的布局(流水布局)
iOS开发UI篇—无限轮播(功能完善)的更多相关文章
- iOS开发UI篇—无限轮播(循环利用)
iOS开发UI篇—无限轮播(循环利用) 一.无限轮播 1.简单说明 在开发中常需要对广告或者是一些图片进行自动的轮播,也就是所谓的无限滚动. 在开发的时候,我们通常的做法是使用一个UIScrollV ...
- iOS开发UI篇—无限轮播(新闻数据展示)
iOS开发UI篇—无限轮播(新闻数据展示) 一.实现效果 二.实现步骤 1.前期准备 (1)导入数据转模型的第三方框架MJExtension (2)向项目中添加保存有“新闻”数据的pli ...
- iOS开发UI篇—无限轮播(循环展示)
iOS开发UI篇—无限轮播(循环展示) 一.简单说明 之前的程序还存在一个问题,那就是不能循环展示,因为plist文件中只有五个数组,因此第一个和最后一个之后就没有了,下面介绍处理这种循环展示问题的小 ...
- 【转】 iOS开发UI篇—UIScrollView控件实现图片轮播
原文:http://www.cnblogs.com/wendingding/p/3763527.html iOS开发UI篇—UIScrollView控件实现图片轮播 一.实现效果 实现图片的自动轮播 ...
- iOS开发UI篇—UIScrollView控件实现图片轮播
iOS开发UI篇—UIScrollView控件实现图片轮播 一.实现效果 实现图片的自动轮播 二.实现代码 storyboard中布局 代码: #import "YYV ...
- iOS开发UI篇—UIScrollView控件实现图片缩放功能
iOS开发UI篇—UIScrollView控件实现图片缩放功能 一.缩放 1.简单说明: 有些时候,我们可能要对某些内容进行手势缩放,如下图所示 UIScrollView不仅能滚动显示大量内容,还能对 ...
- iOS开发UI篇—核心动画(UIView封装动画)
iOS开发UI篇—核心动画(UIView封装动画) 一.UIView动画(首尾) 1.简单说明 UIKit直接将动画集成到UIView类中,当内部的一些属性发生改变时,UIView将为这些改变提供动画 ...
- iOS开发UI篇—CAlayer(创建图层)
iOS开发UI篇—CAlayer(创建图层) 一.添加一个图层 添加图层的步骤: 1.创建layer 2.设置layer的属性(设置了颜色,bounds才能显示出来) 3.将layer添加到界面上(控 ...
- iOS开发UI篇—CALayer简介
iOS开发UI篇—CALayer简介 一.简单介绍 在iOS中,你能看得见摸得着的东西基本上都是UIView,比如一个按钮.一个文本标签.一个文本输入框.一个图标等等,这些都是UIView. 其实 ...
随机推荐
- Datatable的查找和排序(Datatable.Select)
Datatable 是一种常用的数据结构.数据类型有点类似于数据库中的表结构.在没有使用优秀的orm框架前,大部分的数据库的数据都是先变为Datatable 然后再通过代码转换变成 object. ...
- pb数据窗口设置操作
1 使DataWindow列只能追加不能修改如何使DataWindow中的数据只能追加新记录而不能修改,利用 Column 的 Protect 属性可以很方便的做到这一点,方法如下:将每一列的 Pro ...
- html5,进度条
<form action="" id="myform"> <progress value="20" max= ...
- ECMAScript 6教程 (一)
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出 原文连接,博客地址为 http://www.cnblogs.com/jasonnode/ .该系列课程是 ...
- 运行nodejs的blog程序遇见问题
我是运行这个教程的代码.可以在网上找到相关视频和代码. 第一个问题,数据库中没有创建对应的表就开始运行程序.node app.js 这个错误问题大家可以去重现一下 第二个问题,我也没有看明白,但是我根 ...
- 清除dns缓存
Linux清除dns缓存命令 /etc/init.d/dnsmasq restart
- sftp 设置仅能访问自己目录的用户
1. 创建一个目录,owner为root,权限为750或755,此处为 /home/test01 添加一个用户test01,home目录设置为 /home/test01 再创建一个子目录用于用户上传: ...
- Analyzer报表结果行
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- CSS边距---盒子模型
CSS盒子模型 盒子模型主要是有margin(外边距).border(边框).padding(内边距).content(内容)组成,这些属性我们可以把它转移到我们日常生活中的盒子上来理解,日常生活中所 ...
- 【hdu5973】高精度威佐夫博弈
题意:输入a, b表示两堆石头数目,威佐夫博弈,问:先手胜负? a, b <= 1e100. 高精度.当a > b时, a = (a-b)*黄金分割比 时是先手败状态.因为a, b < ...