UICollectionView实现无限轮播
#import "KGNewsCell.h"
#import "KGNews.h"
#import "MJExtension.h"
// 生成一个字符串
#define NSString(...) [NSString stringWithFormat:__VA_ARGS__]
@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;
/** 模型数组 */
@property (strong, nonatomic) NSArray *dataSourceArr;
@property (strong, nonatomic) UIPageControl *pageControl;
@property (strong, nonatomic) NSTimer *timer;
@end
@implementation KGNewsController
- (NSArray *)dataSourceArr {
if (!_dataSourceArr) {
// 模型数组
_dataSourceArr = [KGNews objectArrayWithFilename:@"newses.plist"];
}
return _dataSourceArr;
}
static NSString *cell_id = @"KGNewsCell";
- (void)viewDidLoad {
[super viewDidLoad];
// 注册nib里面的cell
[self.collectionView registerNib:[UINib nibWithNibName:@"KGNewsCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:cell_id];
// 在100组中间开始展示图片
[self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:KGCount / 2] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
// 添加定时器
[self addTimer];
// 添加pageControll
UIPageControl *pageControl = [[UIPageControl alloc] init];
pageControl.center = CGPointMake(self.view.bounds.size.width / 2, 200);
pageControl.pageIndicatorTintColor = [UIColor redColor];
pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
[self.view addSubview:pageControl];
pageControl.numberOfPages = 5;
self.pageControl = pageControl;
}
/**
* 添加定时器
*/
- (void)addTimer {
// 1.创建定时器
// 2.把定时器添加到mainRunLoop(主线程也会抽空处理NSTimer的事件)(如果不添加到mainRunLoop,用户做其他操作的时候,定时器就会停止工作)
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
self.timer = timer;
}
/**
* 移除定时器
*/
- (void)removeTimer {
// 停止定时器
[self.timer invalidate];
// 清空定时器
self.timer = nil;
}
- (void)nextPage {
// 1.马上显示回最中间那组的数据
NSIndexPath *currentIndexPath = [
self resetIndexPath];
// 2.计算下一个需要展示的位置(每次都在100组中间组开始)
NSInteger nextItem = currentIndexPath.item + 1;
NSInteger nextSection = currentIndexPath.section;
if (nextItem == self.dataSourceArr.count) {
nextItem = 0;
nextSection ++;
}
NSIndexPath *nextIndexPath = [NSIndexPath indexPathForItem:nextItem inSection:nextSection];
// 3.通过动画滚动到下一个位置
[self.collectionView scrollToItemAtIndexPath:nextIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
// 4.显示页码
self.pageControl.currentPage = nextItem;
}
/**
* 重置indexPath
*/
- (NSIndexPath *)resetIndexPath {
// 1.当前正在展示的位置
NSIndexPath *currentIndexPath = [[self.collectionView indexPathsForVisibleItems] lastObject];
// 马上显示回最中间那组的数据
NSIndexPath *currentIndexPathReset = [NSIndexPath indexPathForItem:currentIndexPath.item inSection:KGCount / 2];
[self.collectionView scrollToItemAtIndexPath:currentIndexPathReset atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
return currentIndexPathReset;
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return KGCount;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.dataSourceArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
KGNewsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cell_id forIndexPath:indexPath];
// 传给cell模型
cell.news = self.dataSourceArr[indexPath.item];
return cell;
}
#pragma mark - 监听collectionView滚动
/**
* 用户开始拖拽collectionView
*/
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
[self removeTimer];
}
/**
* 当用户停止拖拽(手指离开)
*/
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
[self addTimer];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
int page = (int)(scrollView.contentOffset.x / scrollView.bounds.size.width + 0.5) % self.dataSourceArr.count; self.pageControl.currentPage = page;}- (IBAction)testNSTimer:(UIButton *)sender { for (int i = 0; i < 10; i ++) { NSLog(@"测试:如果NSTimer不加入mainRunLoop的情况下,点击button,计时器是否还会工作--%d", i); }}
UICollectionView实现无限轮播的更多相关文章
- 用UICollectionView实现无限轮播图
用UICollectionView实现无限轮播图 效果 源码 https://github.com/YouXianMing/Animations 细节
- iOS开发之三个Button实现图片无限轮播(参考手机淘宝,Swift版)
这两天使用Reveal工具查看"手机淘宝"App的UI层次时,发现其图片轮播使用了三个UIButton的复用来实现的图片循环无缝滚动.于是乎就有了今天这篇博客,看到“手机淘宝”这个 ...
- iOS:实现图片的无限轮播(二)---之使用第三方库SDCycleScrollView
iOS:实现图片的无限轮播(二)---之使用第三方库SDCycleScrollView 时间:2016-01-19 19:13:43 阅读:630 评论:0 收藏:0 ...
- iOS开发UI篇—无限轮播(循环利用)
iOS开发UI篇—无限轮播(循环利用) 一.无限轮播 1.简单说明 在开发中常需要对广告或者是一些图片进行自动的轮播,也就是所谓的无限滚动. 在开发的时候,我们通常的做法是使用一个UIScrollV ...
- iOS开发UI篇—无限轮播(新闻数据展示)
iOS开发UI篇—无限轮播(新闻数据展示) 一.实现效果 二.实现步骤 1.前期准备 (1)导入数据转模型的第三方框架MJExtension (2)向项目中添加保存有“新闻”数据的pli ...
- iOS开发UI篇—无限轮播(循环展示)
iOS开发UI篇—无限轮播(循环展示) 一.简单说明 之前的程序还存在一个问题,那就是不能循环展示,因为plist文件中只有五个数组,因此第一个和最后一个之后就没有了,下面介绍处理这种循环展示问题的小 ...
- iOS开发UI篇—无限轮播(功能完善)
iOS开发UI篇—无限轮播(功能完善) 一.自动滚动 添加并设置一个定时器,每个2.0秒,就跳转到下一条. 获取当前正在展示的位置. [self addNSTimer]; } -(void)addNS ...
- Swift应用案例 1.无限轮播
从今天开始,我学习的重点开始转向Swift,并且会分享一些自己学习的心得体会,今天给大家带来的的是无限轮播.广告页的无限轮播是非常常见的一个功能,大多数APP都有,大多数程序员也都实现过,今天我们 ...
- iOS 两种不同的图片无限轮播
代码地址如下:http://www.demodashi.com/demo/11608.html 前记 其实想写这个关于无限轮播的记录已经很久很久了,只是没什么时间,这只是一个借口,正如:时间就像海绵, ...
随机推荐
- P5346 【XR-1】柯南家族
题目地址:P5346 [XR-1]柯南家族 Q:官方题解会咕么? A:不会!(大雾 题解环节 首先,我们假设已经求出了 \(n\) 个人聪明程度的排名. \(op = 1\) 是可以 \(O(1)\) ...
- hp惠普工作站 安装ESXI 从U盘启动黑屏问题
[背景] 公司新买了一套hp z238 workstation需要安装esxi,我按照以前的方法制作好U盘后插入从U盘引导,显示屏一直黑屏,这个问题困扰了我一天....在网上找了好久无果 第二天终于在 ...
- CentOS与Ubuntu修改主机名
CentOS 1.执行hostname查看主机名 2.hostname + 主机名 使需要修改的主机名立即生效,但是下次重启会失效,故需要执行第三步 3.vim /etc/sysconfig/net ...
- NET Core 2.1 Preview 1
NET Core 2.1 Preview 1 [翻译] .NET Core 2.1 Preview 1 发布 原文: Announcing .NET Core 2.1 Preview 1 今天,我们宣 ...
- 《深入理解java虚拟机》笔记(4)对象已死吗
一.垃圾回收器回收的对象 虚拟机内存区域中程序计数器.虚拟机栈.本地方法栈随线程而生,随线程而灭.这3个区域内存分配和回收都具备确定性.因此不需要过多考虑回收问题. 而Java堆和方法区不一样,这部分 ...
- vue.js数据绑定语法
原始高清大图下载 1.数据绑定 html代码: <div id="first" class="first">msg:{{msg}}</div& ...
- 使用jstack分析解决进程死锁问题
项目启动后不久就会出现死锁的现象,一直不知道什么原因造成的,后来经过大神的指点,解决了这个问题. 流程如下: 1.环境jdk1.6以上: 2.linux下使用ps aux|grep tomcat 命令 ...
- 百度地图API的基本用法
首先 ,如果想调用百度地图api,你需要获取一个百度地图api的密钥. 申请秘钥的步骤: 1.搜索百度地图: 2.进入后,先登录然后点击申请密钥: 3. 4.申请成功,拥有密钥 有了密钥之后,引入百度 ...
- win7 dos窗口模拟帧刷新
前几天是白色情人节,临时脑抽写了个表白神器 高端大气上档次,就是不知道该送给谁,经过两天的反射弧思考决定还是写给博客娘吧.- -~ 这个程序就是打开后,在Dos窗口内模拟写出几行字母.其实主要就是模拟 ...
- WebView全面学习(二)-- Native与js双方通信
WebView全面学习(二)-- Native与js双方通信 Native与js通信的本质 Native与js通信的核心在于WebView. 两端的通信主要还是单向的.假如要完成js->Nati ...