UICollectionView 用作轮番图的实现,demo 地址:https://github.com/SummerHH/YJCYCleCollectionVIew

#import <UIKit/UIKit.h>

@interface YJCycleView : UIView

@property (nonatomic, strong) NSArray *cycleArr;
@end
#import "YJCycleView.h"
#import "UIView+Extension.h"
#import "YJCycleCollectionViewCell.h" static NSString *const cycleCell = @"cycleCell";
@interface YJCycleView ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, strong) UIPageControl *pageControl;
@property (nonatomic, strong) UICollectionView *collectionView; @end
@implementation YJCycleView - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { [self initView];
}
return self;
} - (void)setCycleArr:(NSArray *)cycleArr { _cycleArr = cycleArr; [self.collectionView reloadData];
self.pageControl.numberOfPages = cycleArr.count;
//*10向左滑多少个
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:cycleArr.count * inSection:];
[self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:NO]; [self removeCycleTimer];
[self addCycleTimer]; } - (void)initView {
self.autoresizingMask = UIViewAutoresizingNone;
[self addSubview:self.collectionView];
[self addSubview:self.pageControl];
} - (UICollectionView *)collectionView { if (_collectionView == nil) { UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.minimumLineSpacing = ;
layout.minimumInteritemSpacing = ;
layout.itemSize = CGSizeMake(self.width, self.height);
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(, , self.width, self.height) collectionViewLayout:layout];
_collectionView.delegate = self;
_collectionView.dataSource = self;
_collectionView.showsHorizontalScrollIndicator = NO;
_collectionView.showsVerticalScrollIndicator = NO;
_collectionView.pagingEnabled = YES;
[_collectionView registerNib:[UINib nibWithNibName:@"YJCycleCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:cycleCell];
} return _collectionView;
} - (UIPageControl *)pageControl { if (_pageControl == nil) {
_pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake((self.width/)-(self.cycleArr.count*)/, self.height-, , )];
_pageControl.currentPage = ;
[_pageControl setPageIndicatorTintColor:[UIColor whiteColor]];
[_pageControl addTarget:self action:@selector(pageMove:) forControlEvents:UIControlEventValueChanged];
}
return _pageControl;
} - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { //*1000设置最大值
return self.cycleArr.count*;
} - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { YJCycleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cycleCell forIndexPath:indexPath]; if (self.cycleArr.count !=) {
cell.imageView.image = [UIImage imageNamed:self.cycleArr[indexPath.item % self.cycleArr.count]];
}
cell.backgroundColor = indexPath.item % == ? [UIColor redColor] : [UIColor yellowColor];
return cell;
} - (void)pageMove:(UIPageControl *)page {
CGFloat currentOffSetX = self.collectionView.contentOffset.x;
CGFloat offSetX = currentOffSetX + self.collectionView.width * self.pageControl.currentPage; [self.collectionView setContentOffset:CGPointMake(offSetX, ) animated:YES];
} - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { [self removeCycleTimer];
} - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { [self addCycleTimer];
} - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat ofSetX = scrollView.contentOffset.x + scrollView.bounds.size.width * 0.5;
self.pageControl.currentPage = (int)(ofSetX / scrollView.bounds.size.width) % (self.cycleArr.count);
} - (void)removeCycleTimer
{
// 移除定时器
[self.timer invalidate];
self.timer = nil;
} - (void)addCycleTimer { self.timer = [NSTimer timerWithTimeInterval:3.0f target:self selector:@selector(scrollToNext) userInfo:nil repeats:YES]; [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
} - (void)scrollToNext {
CGFloat currentOffSetX = self.collectionView.contentOffset.x;
CGFloat offSetX = currentOffSetX + self.collectionView.width; [self.collectionView setContentOffset:CGPointMake(offSetX, ) animated:YES];
}

scrollerView 轮番图的更多相关文章

  1. UIcollectionView 实现 轮番图

    UICollectionView 用作轮番图的实现,demo 地址:https://github.com/SummerHH/YJCYCleCollectionVIew #import <UIKi ...

  2. jq轮播图

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. Servlet与Jsp的结合使用实现信息管理系统一

    PS:1:先介绍一下什么是Servlet? Servlet(Server Applet)是Java Servlet的简称,称为小服务程序或服务连接器,用Java编写的服务器端程序,主要功能在于交互式地 ...

  4. css div布局示例1(head-main-footer)

    很简单的文档流布局 <!doctype html> <html lang="en"> <head> <meta charset=" ...

  5. swiper 视频轮番

    百度搜索:swiper 视频轮番 转载1:https://blog.csdn.net/Aimee1608/article/details/79637929 项目中使用swiper插件嵌套video标签 ...

  6. UIScrollerView当前显示3张图

    代码地址如下:http://www.demodashi.com/demo/11173.html WSLScrollView 功能描述:这是在继承UIView的基础上利用UIScrollerView进行 ...

  7. 【微信小程序】获取轮播图当前图片下标、滑动展示对应的位数、点击位数展示对应图片

    业务需求: 3个图片轮番播放,可以左右滑动,点击指示点可以切换图片  index.wxml: 这里使用小程序提供的<swiper>组件autoplay:自动播放interval:自动切换时 ...

  8. Android-使用ViewFlipper实现轮番切换广告栏

    所谓的轮番切换广告栏,指的是下面这个东西,笔主不知道该怎么确切描述这货... 笔主没有百度研究过其他大牛是怎么实现这个功能的,在这里笔主充分发挥DIY精神,利用ViewFlipper闭门土制了一个,下 ...

  9. 纯CSS实现轮播图效果,你不知道的CSS3黑科技

    前言 轮播图已经是一个很常见的东西,尤其是在各大App的首页顶部栏,经常会轮番显示不同的图片. 一提到轮播图如何实现时,很多人的第一反应就是使用Javascript的定时器,当然这种方法是可以实现的. ...

随机推荐

  1. C#中的堆和栈

    看到一篇讲堆和栈的文章,是我目前为止见到讲的最易懂,详细和深入的.我翻译成中文.以此总结. 原文=>C#Heap(ing) Vs Stack(ing) in .NET 在net framewor ...

  2. Google C++ 编程规范总结

    一.头文件 #define 的保护 项目 foo 中的头文件 foo/src/bar/baz.h 按如下方式保护: #ifndef FOO_BAR_BAZ_H_ #define FOO_BAR_BAZ ...

  3. Hadoop学习笔记2---配置详解

    配置系统是复杂软件必不可少的一部分,而Hadoop配置信息处理是学习Hadoop源代码的一个很好的起点.现在就从Hadoop的配置文件谈起. 一.Hadoop配置格式 Hadoop配置文件格式如下所示 ...

  4. HDU-4747 Mex 线段树

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4747 题意:求一个数列中,所有mex(L,R)的和. 注意到mex是单调不降的,那么首先预处理出mex ...

  5. JavaFx2.0中CSS的应用

    http://user.qzone.qq.com/773534839#!app=2&via=QZ.HashRefresh&pos=1326994508 ———————————————— ...

  6. C++问题-无法打开包括文件:“GLES2/gl2.h”

    资料来源:http://blog.csdn.net/weizehua/article/details/12623719http://tieba.baidu.com/p/2747715029 问题经过: ...

  7. Oracle中纵横表的转化

    横表就是普通的建表方式,如一个表结构为:主键.字段1.字段2.字段3......如果变成纵表后, 则表结构为:主键.字段代码.字段值.而字段代码则为字段1.字段2.字段3. 纵表对从数据库到内存的映射 ...

  8. thymeleaf中的th:with用法

    局部变量,th:with能定义局部变量: <div th:with="firstPer=${persons[0]}"> <p> The name of th ...

  9. 转载JQuery 中empty, remove 和 detach的区别

    转载 http://www.cnblogs.com/lisongy/p/4109420.html .empty()  描述: 从DOM中移除集合中匹配元素的所有子节点. 这个方法不接受任何参数. 这个 ...

  10. SQL Server 2008 Data Types and Entity Framework 4

    Because I’ve had a lot of conversations about spatial data types lately, I thought I would create a ...