01.轮播图之四 :imageViews(2 或者 3 个imageview) 轮播
首先说下 3 个imageView的轮播,这个逻辑分析起来 比较简单,
先上传个图片吧:::::
主要分析起来,核心就是这样 :新的图片永远是加在中间ImageView上的,下一轮的第一张图片,是上一轮的第二张图片,这样就可以形成一个无缝滚动了,
只是切换了数据的逻辑。难理解就算了,看代码才容易懂………………
.h 的声明文件
@interface ImageViewShuffling : UIView @property (nonatomic,strong)NSArray *array; @end
.m 的具体实现,这里是理解这个逻辑的好地方————————
@interface ImageViewShuffling ()<UIScrollViewDelegate> @property (nonatomic,strong)UIScrollView *scrollView; @property (nonatomic,strong)UIImageView *leftImageView;
@property (nonatomic,strong)UIImageView *centerImageView;
@property (nonatomic,strong)UIImageView *rightImageView; @property (nonatomic,assign)NSInteger currentIndex; @end @implementation ImageViewShuffling
@synthesize array = _array; -(instancetype)initWithFrame:(CGRect)frame{ if (self == [super initWithFrame:frame]) {
self.currentIndex = ;
}
return self;
} -(UIScrollView*)scrollView{ if (_scrollView == nil) {
CGRect scrollRect = CGRectMake(, , self.frame.size.width, self.frame.size.height);
_scrollView = [[UIScrollView alloc]initWithFrame:scrollRect];
[self addSubview:_scrollView];
_scrollView.pagingEnabled = YES;
_scrollView.delegate = self;
}
return _scrollView;
} -(void)setArray:(NSArray *)array{ NSAssert(array.count != , @"传入的滚动数组是 空的");
_array = array;
[self initImageViews];
} -(void)initImageViews{ self.leftImageView = [[UIImageView alloc]initWithFrame:CGRectMake(, , self.frame.size.width, self.frame.size.height)];
self.centerImageView = [[UIImageView alloc]initWithFrame:CGRectMake(self.frame.size.width, , self.frame.size.width, self.frame.size.height)];
self.rightImageView = [[UIImageView alloc]initWithFrame:CGRectMake(self.frame.size.width*, , self.frame.size.width, self.frame.size.height)];
self.leftImageView.contentMode = UIViewContentModeScaleAspectFit;
self.centerImageView.contentMode = UIViewContentModeScaleAspectFit;
self.rightImageView.contentMode = UIViewContentModeScaleAspectFit; [self.scrollView addSubview:self.leftImageView];
[self.scrollView addSubview:self.centerImageView];
[self.scrollView addSubview:self.rightImageView];
/*
千万不要忘记设置这里
*/
self.scrollView.contentSize = CGSizeMake(self.frame.size.width * , );
self.scrollView.contentOffset = CGPointMake(self.frame.size.width , ); [self setImageWith:self.currentIndex];
} -(void)setImageWith:(NSInteger)integer{ self.centerImageView.backgroundColor = (UIColor*)self.array[integer];
self.leftImageView.backgroundColor = (UIColor*)self.array[(integer - + self.array.count)%(self.array.count)];
self.rightImageView.backgroundColor = (UIColor*)self.array[(integer + )%(self.array.count)];
}
/**
理解此处,你就能理解整个逻辑实现
*/
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ if (scrollView == self.scrollView) { if (scrollView.contentOffset.x > self.frame.size.width ) {//右滑 self.currentIndex = (self.currentIndex+) % (self.array.count); }else if(scrollView.contentOffset.x < self.frame.size.width ){//左滑 self.currentIndex = (self.currentIndex - + self.array.count)%(self.array.count); }else{// }
[self setImageWith:self.currentIndex];
self.scrollView.contentOffset = CGPointMake(self.frame.size.width , );
}
}
上面就是 3张图实现的 轮播图,童叟无欺………………
调用::::
-(void)prepareImageViewShuffling{ ImageViewShuffling *imageViewShuffling = [[ImageViewShuffling alloc]initWithFrame:CGRectMake(, , self.view.frame.size.width -, )];
[self.view addSubview:imageViewShuffling];;
imageViewShuffling.array = self.arr;
}
********************************************************************************
弄懂了上面这个,相信下面介绍这个两张图片实现的轮播 ,对你就是小case
他就是对上面3张图片的简化…………
看完上面的逻辑,理解后发现, 左侧,或者右侧的 imageView 始终有一个是备用的,就像实现的代码一样,你不是向左就是向右滑动;
根据这个理由,就想到了这个,实时检测scroll 的滚动方向,这样就可以实现:一个主要显示的imageView,和一个 待显示的imageView……
越说越啰嗦,直接撸代码……
.h 文件
@interface ImageView2Shuffling : UIView
@property (nonatomic,strong)NSArray *array; @end
.m 实现文件,逻辑变化不大
@interface ImageView2Shuffling ()<UIScrollViewDelegate> @property (nonatomic,strong)UIScrollView *scrollView;
@property (nonatomic,strong)UIImageView *currentImageView;
@property (nonatomic,strong)UIImageView *otherImageView;
@property (nonatomic,assign)NSInteger currentIndex;
/*
这个是检测滚动方向的
*/
@property (nonatomic,strong)NSString *directionString;
@end @implementation ImageView2Shuffling
@synthesize array = _array; -(instancetype)initWithFrame:(CGRect)frame{ if (self == [super initWithFrame:frame]) {
self.currentIndex = ;
self.directionString = [[NSString alloc]init];
}
return self;
} -(UIScrollView*)scrollView{ if (_scrollView == nil) {
CGRect scrollRect = CGRectMake(, , self.frame.size.width, self.frame.size.height);
_scrollView = [[UIScrollView alloc]initWithFrame:scrollRect];
[self addSubview:_scrollView];
_scrollView.pagingEnabled = YES;
_scrollView.delegate = self;
}
return _scrollView;
} -(void)setArray:(NSArray *)array{ NSAssert(array.count != , @"传入的滚动数组是 空的");
_array = array;
[self initImageViews];
} -(void)initImageViews{
/*
这里写个空 frame 的imageview
*/
self.otherImageView = [[UIImageView alloc]init];
[self.scrollView addSubview:self.otherImageView]; self.currentImageView = [[UIImageView alloc]initWithFrame:CGRectMake(self.frame.size.width, , self.frame.size.width, self.frame.size.height)];
self.currentImageView.contentMode = UIViewContentModeScaleAspectFit;
[self.scrollView addSubview:self.currentImageView]; self.scrollView.contentSize = CGSizeMake(self.frame.size.width * , );
self.scrollView.contentOffset = CGPointMake(self.frame.size.width , ); [self setImageWith:self.currentIndex];
} -(void)setImageWith:(NSInteger)integer{ self.currentImageView.backgroundColor = (UIColor*)self.array[integer];
} -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ if (scrollView == self.scrollView) { self.directionString = @""; if (scrollView.contentOffset.x > self.frame.size.width ) {//右滑 self.currentIndex = (self.currentIndex+) % (self.array.count); }else if(scrollView.contentOffset.x < self.frame.size.width ){//左滑 self.currentIndex = (self.currentIndex - + self.array.count)%(self.array.count);
}else{//
}
[self setImageWith:self.currentIndex];
self.scrollView.contentOffset = CGPointMake(self.frame.size.width , );
}
} /*
这里集中了些 修改的代码
检测滚动方向,改变otherImageview 的位置,和颜色
*/
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{ if (scrollView == self.scrollView) { if (scrollView.contentOffset.x > self.frame.size.width ) {//右滑 [self refreshOtherImageViewWithDirectionString:@"right"];
}else if(scrollView.contentOffset.x < self.frame.size.width ){//左滑 [self refreshOtherImageViewWithDirectionString:@"left"];
}else{//
}
}
} -(void)refreshOtherImageViewWithDirectionString:(NSString*)direction{ if ([self.directionString isEqualToString:direction]) {
return ;
}
self.directionString = direction; CGRect frame; NSInteger integer;
if ([direction isEqualToString:@"right"]) { integer = (self.currentIndex+) % (self.array.count);
frame = CGRectMake(self.frame.size.width*, , self.frame.size.width, self.frame.size.height);
}else{ //if "left" integer = (self.currentIndex - + self.array.count)%(self.array.count);
frame = CGRectMake(, , self.frame.size.width, self.frame.size.height);
}
self.otherImageView.backgroundColor = (UIColor*)self.array[integer];
self.otherImageView.frame = frame;
}
写到这里,应该可以了,简单来讲,就是用一个direction 替换了一个imageview ,应该算减少了UI 的开销,增加了逻辑判断和运算
调用::::
-(void)prepareImageView2Shuffling{ ImageView2Shuffling *imageView2Shuffling = [[ImageView2Shuffling alloc]initWithFrame:CGRectMake(, , self.view.frame.size.width -, )];
[self.view addSubview:imageView2Shuffling];;
imageView2Shuffling.array = self.arr;
}
突然感觉要超额完成任务的节拍,为自己加油点赞………………
01.轮播图之四 :imageViews(2 或者 3 个imageview) 轮播的更多相关文章
- 纯js轮播图练习-2,js+css旋转木马层叠轮播
基于css3的新属性,加上js的操作,让现在js轮播图花样越来越多. 而现在出现的旋转木马层叠轮播的轮播图样式,却是得到了很多人都喜爱和投入使用. 尤其是在各大软件中,频繁的出现在大家的眼里,在web ...
- 从零开始,搭建博客系统MVC5+EF6搭建框架(5),博客详情页、留言、轮播图管理、右侧统计博文
一.博客系统进度回顾 上一遍博客介绍到,系统已经实现到了发布以及前台布局展示,接下来就是实现一些,详情页,留言.轮播图管理.右侧博文统计信息实现. 二.博客系统详情页实现 2.1先来看看详情页展示的效 ...
- 2.bootstrap练习笔记-轮播图
bootstrap练习笔记-轮播图 1.要使用轮播图,首先要将其放在一个主div里面 设置id为myCaroysel class为carousel slide 设置id是标识这个div是轮播图,等到l ...
- Android开发之ViewPager实现轮播图(轮播广告)效果的自定义View
最近开发中需要做一个类似京东首页那样的广告轮播效果,于是采用ViewPager自己自定义了一个轮播图效果的View. 主要原理就是利用定时任务器定时切换ViewPager的页面. 效果图如下: 主页面 ...
- android 轮播图
轮播图是很常用的一个效果 核心功能已经实现 没有什么特殊需求 自己没事研究的 所以封装的不太好 一些地方还比较糙 为想要研究轮播图的同学提供个参考 目前测试图片为mipmap中的图片 没有写从网络加载 ...
- 原生js实现简单移动端轮播图
最近项目不是很忙,自己就用原生js写了一个简单的移动端轮播图的小demo,可实现自动轮播和手势滑动轮播,然后就把它记录到个人博客里.还有很多不足的地方,希望多多指出,以便改进. 1.代码部分 分为四个 ...
- Android轮播图
轮播图是很常用的一个效果 核心功能已经实现 没有什么特殊需求 自己没事研究的 所以封装的不太好 一些地方还比较糙 为想要研究轮播图的同学提供个参考目前测试图片为mipmap中的图片 没有写从网络加载图 ...
- 浅谈轮播图(原生JavaScript实现)
现在各种轮播图插件,玲琅满目,用起来也非常方便,通常只需要选择元素然后传入参数就可以了.但是,和授人以鱼不如授人以渔一样的道理,不管怎样最基本的轮播图原理还是应当掌握的.这样不仅有利于我们自己写出来满 ...
- 原生JS实现简易轮播图
原生JS实现简易轮播图(渐变?) 最近做网页总是会用到轮播图,我就把之前写的轮播图单独拿出来吧,如果有...如果真的有人也需要也可以复制去用用啊..哈~.. window.onload = funct ...
随机推荐
- Jfinal undertow本地运行加载静态文件
undertow部署文档中可配置静态资源也可以添加磁盘目录作为项目中得静态文件访问 undertow.resourcePath = src/main/webapp, D:/static 这样配置就可以 ...
- Z+F激光扫描仪
链接:https://zhuanlan.zhihu.com/p/48589754 三维扫描仪有三个误差来源: ● 线性误差(激光雷达部分/LARA) ● 测距噪声(激光雷达部分/LARA) ● 测角误 ...
- tsql 通过row_number() over() 产生行号
先按userIP分组,再按时间排序,最后编号. select row_number() over (partition by UserIp order by insertTime),* from us ...
- java.sql.SQLException: connection holder is null;
一.问题来源分析 出现的错误 : Cause: java.sql.SQLException: connection holder is null; uncategorized SQLException ...
- HTML 文字剧中
HTML 内想使文字剧中的办法 就是 text-align:center 剧中前效果图 剧中后效果图 代码:
- bzoj 4240: 有趣的家庭菜园 树状数组+贪心
有一个小性质:就是一个下标排列的最小移动次数就是逆序对数. 我们发现最终形态一定是一个波峰. 那么我们求的就是形成波峰的下标最少逆序对数. 考虑将元素从小到大依次插入. 那么,对于第 $i$ 个元素, ...
- 07_Kibana界面操作ES
Kibana界面的API操作ES 1.创建索引 1.1 指定分片数量和备份数量 1.2 创建默认 2. 查看索引 2.1 查看单个索引设置 2.2 查看所有索引设置 3.文档管理 3.1 添加文档 3 ...
- 爬虫(八):分析Ajax请求抓取今日头条街拍美图
(1):分析网页 分析ajax的请求网址,和需要的参数.通过不断向下拉动滚动条,发现请求的参数中offset一直在变化,所以每次请求通过offset来控制新的ajax请求. (2)上代码 a.通过aj ...
- git add 不能提交 vendor下面的一个文件夹
项目要用grpc.然后composer require XXX. 把对应的包拉倒vendor目录下面.(这里先不考虑要把vendor composer.lock提交到版本库的问题) 然后开发完成后 ...
- 解决Navicat无法连接到centos上的MySQL,但命令行可以,修改权限,MySQL密码权限受限:ERROR 1820 (HY000) ERROR 1819 (HY000)
问题分析 查看MySQL文档发现5.7版本后加入了对用户密码严格的管理规范,具体设置字段如下: validate_password_dictionary_file #插件用于验证密码强度的字典文件路径 ...