scrollerView 轮番图
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 轮番图的更多相关文章
- UIcollectionView 实现 轮番图
UICollectionView 用作轮番图的实现,demo 地址:https://github.com/SummerHH/YJCYCleCollectionVIew #import <UIKi ...
- jq轮播图
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Servlet与Jsp的结合使用实现信息管理系统一
PS:1:先介绍一下什么是Servlet? Servlet(Server Applet)是Java Servlet的简称,称为小服务程序或服务连接器,用Java编写的服务器端程序,主要功能在于交互式地 ...
- css div布局示例1(head-main-footer)
很简单的文档流布局 <!doctype html> <html lang="en"> <head> <meta charset=" ...
- swiper 视频轮番
百度搜索:swiper 视频轮番 转载1:https://blog.csdn.net/Aimee1608/article/details/79637929 项目中使用swiper插件嵌套video标签 ...
- UIScrollerView当前显示3张图
代码地址如下:http://www.demodashi.com/demo/11173.html WSLScrollView 功能描述:这是在继承UIView的基础上利用UIScrollerView进行 ...
- 【微信小程序】获取轮播图当前图片下标、滑动展示对应的位数、点击位数展示对应图片
业务需求: 3个图片轮番播放,可以左右滑动,点击指示点可以切换图片 index.wxml: 这里使用小程序提供的<swiper>组件autoplay:自动播放interval:自动切换时 ...
- Android-使用ViewFlipper实现轮番切换广告栏
所谓的轮番切换广告栏,指的是下面这个东西,笔主不知道该怎么确切描述这货... 笔主没有百度研究过其他大牛是怎么实现这个功能的,在这里笔主充分发挥DIY精神,利用ViewFlipper闭门土制了一个,下 ...
- 纯CSS实现轮播图效果,你不知道的CSS3黑科技
前言 轮播图已经是一个很常见的东西,尤其是在各大App的首页顶部栏,经常会轮番显示不同的图片. 一提到轮播图如何实现时,很多人的第一反应就是使用Javascript的定时器,当然这种方法是可以实现的. ...
随机推荐
- 内核源码分析之tasklet(基于3.16-rc4)
tasklet是在HI_SOFTIRQ和TASKLET_SOFTIRQ两个软中断的基础上实现的(它们是在同一个源文件中实现,由此可见它们的关系密切程度),它的数据结构和软中断比较相似,这篇博文将分析t ...
- ubuntu开机自启动脚本编写
1.将启动脚本复制到/etc/init.d目录下面 2.chmod 755 /etc/init.d/xxx 3.sudo update-rc.d /etc/init.d/xxx defaults 95 ...
- 软件开发杂谈之从需求到上线---valen
背景 IT已经成为当代企业必不可少的竞争手段,从无到有到标配,可以说以后不懂IT的就是文盲这句一点也不过,而软件开发是个复杂工程,零零碎碎各种理论工具和技巧,一言难尽. 本文意在言简意赅,简述软件开发 ...
- Java多线程编程模式实战指南:Active Object模式(上)
Active Object模式简介 Active Object模式是一种异步编程模式.它通过对方法的调用与方法的执行进行解耦来提高并发性.若以任务的概念来说,Active Object模式的核心则是它 ...
- cocos2d-x生成随机数
//获取系统时间 //time_t是long类型,精确到秒,通过time()函数可以获得当前时间和1970年1月1日零点时间的差 time_t tt; ...
- SpringMVC 避免IE执行AJAX时,返回JSON出现下载文件
<?xml version="1.0" encoding="UTF-8"?> <!-- SpringMVC配置文件 --> <be ...
- Linux下的scp拷贝命令详解
相同Linux系统中对文件复制拷贝可以用CP命令: cp [options] source dest cp [options] source... directory 说明:将一个档案拷贝至另一档案, ...
- 201. Segment Tree Build
最后更新 二刷 08-Jan-2017 一刷忘了什么时候做的,只是觉得这几个题挺好的,一步一步手动构建线段树,最终理解了这个数据结构,并且后面有利用的地方. 其实重要的3个东西题目已经提供了: 1) ...
- Unity3D音乐音效学习笔记
对于Unity3D的音乐音效这块一直没有好好的看过,现在准备好好的研究一下,并作为一个笔记记录下. 支持格式 在游戏中,一般存在两种音乐,一种是时间较长的背景音乐,一种是时间较短的音效(比如按钮点击, ...
- WinDbg x 64 使用 SOS: 无法找到运行时 DLL (clr.dll)
http://www.datazx.cn/Forums/en-US/59aa78c9-dc05-43c8-9efe-e7b132056afc/action?threadDisplayName=win ...