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. MySQL数据库备份和还原

    备份MySQL数据库的命令 mysqldump -hhostname -uusername -ppassword databasename > backupfile.sql 备份MySQL数据库 ...

  2. Java——观察者模式实例

    观察者模式(订阅/发布模式) 作者: 代码大湿 代码大湿 Java中观察者模式中主要是Observerable类(被观察者),和Observer接口(观察者).下面是个简单的demo //被观察者 p ...

  3. [iOS UI进阶 - 6.1] 核心动画CoreAnimation

    A.基本知识 1.概念 Core Animation是一组非常强大的动画处理API,使用它能做出非常炫丽的动画效果,而且往往是事半功倍,使用它需要先添加QuartzCore.framework和引入对 ...

  4. Chrome的Postman的使用

    Chrome提供了一个很好的Web App 名为 Postman 使用这个web app,你可以输入一个url,然后可以很清楚的看到返回的各种结果 直接在Google中输入Postman, 找到它   ...

  5. poj3687

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9028   Accepted: 2444 De ...

  6. Android之基于HTTP协议的下载

    Android之基于HTTP协议的下载 http://www.blogjava.net/zh-weir/archive/2010/05/02/319892.html http://www.qianfa ...

  7. python 实现冒泡排序与快速排序 遇到的错误与问题

    今天看了兄弟连php里面的冒泡排序与快速排序,想了下应该可以用python实现. 冒泡排序函数: def mysort(x): len1 = len(x) for i in range(len1-1, ...

  8. IIS功能查看、配置

    #获取所有IIS功能列表: get-webconfiguration -filter /system.webserver #查看目录浏览功能的配置信息:Get-WebConfiguration -fi ...

  9. MX记录查询

    nslookup set type=mx

  10. webpack echarts配置实例

    简单介绍 本例介绍怎样在webpack中构建依赖echats的项目,echarts有好几种方式引入项目: 标签单文件引入:自1.3.5開始,ECharts提供标签式引入.假设项目本身并非基于模块化开发 ...