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的定时器,当然这种方法是可以实现的. ...
随机推荐
- Excel动态生成JSON
在最近的一个项目中,有大量的数据源来至Excel,转成JSON供前台使用.Excel数据是人工录入的,难免会有错误,所以中间会有逻辑检查.在C#中读取Excel的方式有很多,网上一搜一大堆,这里我也贴 ...
- 在EC2上安装MEAN环境
本文在个人博客上的地址为URL,欢迎品尝. 搭建决策树项目外网DEMO尝试几个地方后,最后选择了EC2(Amazon Elastic Compute Cloud).选择的是最经济便宜的Amazon L ...
- Linux cat命令参数及使用方法详解
cat是Linux系统下用来查看文件连续内容用的指令,字面上的含意是“concatenate”(连续)的缩写.除了用来作为显示文件内容外,cat指令也可用于标准流上的处理,如将显示的信息转入或附加另一 ...
- (转载)Linux启动过程详解
启动第一步--加载BIOS当你打开计算机电源,计算机会首先加载BIOS信息,BIOS信息是如此的重要,以至于计算机必须在最开始就找到它.这是因为BIOS中包含了CPU的相关信息.设备启动顺序信息.硬盘 ...
- 【转】iOS 浅谈:深.浅拷贝与copy.strong
深.浅拷贝 copy mutableCopy NSString 1 2 3 4 5 6 NSString *string = @"汉斯哈哈哈"; // 没有产生新对象 NSStri ...
- 10个专属于移动app开发者的最佳移动JavaScript框架
1.Titanium Mobile JavaScript Frameworks Titanium Mobile JavaScript框架是移动应用开发者(Android & iOS)首选的最优 ...
- 教程-在F9后提示内存错误,点击了乎略,之后怎么取消乎略?
问题现象:F9后,调试程序,提示内存错误,点击了“乎略”.之后再也没有出现错误了.可是想改这个BUG时,没法取消乎略了. 问题原因:在DLEPHI的选项中是这么一个地方是可以设置的. 问题处理:打开D ...
- Redis安装与调试
Redis安装与调试 Redis安装与调试linux版本:64位CentOS 6.5 Redis版本:2.8.17 (更新到2014年10月31日) Redis官网:http://redis.io/ ...
- 命令行刷机教程( 以Linux系统为例 )
//第一步adb device // 如果不能cd AndroidSDK/platform-toolsadb kill-server adb start-server //第二步adb reboot ...
- hdu 4781 Assignment For Princess (2013ACMICPC 成都站 A)
http://acm.hdu.edu.cn/showproblem.php?pid=4781 由于题目太长,这里就不直接贴了,直接说大意吧. 题目大意:有一个n个点,m条边的有向图,每条边的权值分别为 ...