iOS -- 轮播图
UIScrollView + 多张 ImageView 实现轮播
实现原理:
将所有图片的名字储存在数组 imageAry 中,imageAry 的元素个数为 num,在 scrollView 上添加 num + 1 个 UIImageView,第一个 imageView 上放最后一张图片,第二个 imageView 上放第一张图片,依次类推,最后一个 imageView 上的图片和第一个 imageView 的图片相同,用来过渡。在初始状态将 scrollView 的 contentOffset 的 x 值设为一个屏幕的宽度。
代码如下:
for (NSInteger i = 0; i < count + 1; i++) {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * DEVICE_WIDTH, 0, DEVICE_WIDTH, 500)];
NSString *imageName = self.imageAry[count-1];
if (i != 0) {
imageName = self.imageAry[i-1];
}
imageView.image = [UIImage imageNamed:imageName];
[self.scrollView addSubview:imageView];
}
手动轮播
当手动滚动到倒数第二张图片,继续向右滚动时,设置 scrollView 的 contentOffset 的 x 值为 0,并不做动画,实现自然过渡到第一个 imageView,显示最后一张图片。
当手动滚动到第一个 imageView,继续向左滚动时,设置 scrollView 的 contentOffset 的 x 值为图片的总个数乘以屏幕的宽度,并不做动画,实现自然过渡到最后一个 imageView,显示的也是最后一张图片。
代码如下:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat offsetX = scrollView.contentOffset.x;
NSInteger count = self.imageAry.count;
if (offsetX < 0) {
[scrollView setContentOffset:CGPointMake(count * DEVICE_WIDTH, 0) animated:NO];
} else if (offsetX > count * DEVICE_WIDTH) {
[scrollView setContentOffset:CGPointMake(0, 0) animated:NO];
}
}
自动轮播
当自动滚动到倒数第二张图片,继续向右滚动时,设置 scrollView 的 contentOffset 的 x 值为 0,并不做动画,紧接着设置 scrollView 的 contentOffset 的 x 值为屏幕的宽度,并做动画,实现从最后一个 imageView 到第一个 imageView 的自然过渡。
代码如下:
- (void)autoPlay {
[super autoPlay];
CGFloat offsetX = self.scrollView.contentOffset.x;
NSInteger count = self.imageAry.count;
CGFloat imageViewX = DEVICE_WIDTH;
if (offsetX > (count - 1) * DEVICE_WIDTH) {
[self.scrollView setContentOffset:CGPointMake(0, 0) animated:NO];
} else {
imageViewX += offsetX;
}
[self.scrollView setContentOffset:CGPointMake(imageViewX, 0) animated:YES];
}
UIScrollView + 3 张 ImageView 实现轮播
实现原理:
将所有图片的名字储存在数组 imageAry 中,imageAry 的元素个数为 num,在 scrollView 上添加 3 个 UIImageView,第一个 imageView 上放最后一张图片,第二个 imageView 上放第一张图片,第三个 imageView 上放第二张图片。在初始状态将 scrollView 的 contentOffset 的 x 值设为一个屏幕的宽度,即显示的是中间的 imageView。
代码如下:
for (NSInteger i = 0; i < 3; i++) {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * DEVICE_WIDTH, 0, DEVICE_WIDTH, 500)];
NSString *imageName = self.imageAry[count-1];
if (i != 0) {
imageName = self.imageAry[i-1];
}
imageView.image = [UIImage imageNamed:imageName];
[self.scrollView addSubview:imageView];
}
手动轮播
当 scrollView 的 contentOffset 的 x 值为 0 时,减小 pageControl 的 currentPage(如果 currentPage 为 0,则设置 currentPage 为最大)。
当 scrollView 的 contentOffset 的 x 值为 2 倍的屏幕宽度时,变大 pageControl 的 currentPage(如果 currentPage 为最大,则设置 currentPage 为 0)。
然后根据 currentPage 从数组中取出图片给 imageView 赋值。
最后将 scrollView 的 contentOffset 的 x 值恢复到一个屏幕的宽度,且不做动画,即始终显示中间的 imageView。
代码如下:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat offsetX = scrollView.contentOffset.x;
if (offsetX == 0) {
[self currentPageDown];
[self resetImagesAndContentOffset];
} else if (offsetX == 2 * DEVICE_WIDTH) {
[self currentPageUp];
[self resetImagesAndContentOffset];
}
} - (void)currentPageDown {
NSInteger count = self.imageAry.count;
NSInteger currentIndex = (self.pageControl.currentPage - 1 + count) % count;
self.pageControl.currentPage = currentIndex;
} - (void)currentPageUp {
NSInteger currentIndex = (self.pageControl.currentPage + 1) % self.imageAry.count;
self.pageControl.currentPage = currentIndex;
} - (void)resetImagesAndContentOffset {
NSInteger currentPage = self.pageControl.currentPage;
NSInteger count = self.imageAry.count;
for (NSInteger i = 0; i < 3; i++) {
UIImageView *imageView = self.scrollView.subviews[i];
NSInteger imageIndex = 0;
switch (i) {
case 0:
imageIndex = currentPage - 1;
if (imageIndex < 0) {
imageIndex = count - 1;
}
break;
case 1:
imageIndex = currentPage;
break;
case 2:
imageIndex = currentPage + 1;
if (imageIndex > count - 1) {
imageIndex = 0;
}
break;
}
imageView.image = [UIImage imageNamed:self.imageAry[imageIndex]];
}
[self.scrollView setContentOffset:CGPointMake(DEVICE_WIDTH, 0) animated:NO];
}
自动轮播
设置 scrollView 的 contentOffset 的 x 值为 2 倍的屏幕宽即可。
代码如下:
- (void)autoPlay {
[super autoPlay];
[self.scrollView setContentOffset:CGPointMake(2 * DEVICE_WIDTH, 0) animated:YES];
}
iOS -- 轮播图的更多相关文章
- 一步一步拆解一个简单的iOS轮播图(三图)
导言(可以不看): 不吹不黑,也许是东半球最简单的iOS轮播图拆分注释(讲解不敢当)了(tree new bee).(一句话包含两个人,你能猜到有谁吗?提示:一个在卖手机,一个最近在卖书)哈哈... ...
- IOS轮播图
轮播图播放的主要技术在于: cell的封装.这里采用UICollectionViewCell实现. #import <UIKit/UIKit.h> @interface CircleVie ...
- ReactNative新手学习之路04 组件化开发轮播图swiper支持安卓和IOS
react native 新手之路04 组件化开发轮播图swiper支持安卓和IOS npm install react-native-carousel --save git 地址Properties ...
- iOS 图片轮播图(自动滚动)
iOS 图片轮播图(自动滚动) #import "DDViewController.h" #define DDImageCount 5 @interface DDViewContr ...
- iOS回顾笔记(05) -- 手把手教你封装一个广告轮播图框架
html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,bi ...
- iOS最笨的办法实现无限轮播图(网络加载)
iOS最笨的办法实现无限轮播图(网络加载) 简单的做了一下: 使用方法: 把 请求返回的 图片地址(字符串类型)放进数组中就行 可以使用SDWebImage(我就是用的这个)等..需要自己导入并引用, ...
- iOS开发之 用第三方类库实现轮播图
在github上面有很多的第三方类库,大大节约了大家的开发时间 下载地址:https://github.com/gsdios/SDCycleScrollView 现已支持cocoapods导入:pod ...
- iOS中 轮播图放哪最合适? 技术分享
我们知道,轮播图放在cell或collectionViewCell上会影响用户层级交互事件,并且实现起来比较麻烦,现在推出一个技术点:答题思路是:将UIScrollView放在UIView或UICol ...
- iOS swift版本无限滚动轮播图
之前写过oc版本的无限滚动轮播图,现在来一个swift版本全部使用snapKit布局,数字还是pageConrrol样式可选 enum typeStyle: Int { case pageContro ...
随机推荐
- ramdisk plus v11.5安装内存虚拟硬盘
ramdisk plus v11.5.桌面版操作及应用图解说明 一.ramdisk plus程序安装方法: 1.先安装英文原版软件(RamDisk-desktop.exe桌面版),安装路径不要更改,安 ...
- deepsooncms在Ubuntu 14.04上部署教程
deepsooncms在Ubuntu 14.04上部署教程 一.安装mono1.在命令行运行sudo apt-key adv --keyserver keyserver.ubuntu.com --re ...
- 基于java的分布式爬虫
分类 分布式网络爬虫包含多个爬虫,每个爬虫需要完成的任务和单个的爬行器类似,它们从互联网上下载网页,并把网页保存在本地的磁盘,从中抽取URL并沿着这些URL的指向继续爬行.由于并行爬行器需要分割下载任 ...
- TODO:MongoDB的查询更新删除总结
TODO:MongoDB的查询更新删除总结 常用查询,条件操作符查询,< .<=.>.>=.!= 对应 MongoDB的查询操作符是$lt.$lte.$gt.$gte.$ne ...
- FASTDFS调研报告(V1.0)
之前的文章,现在放出来,以供参阅. 一.fastdfs简介 FastDFS是一个轻量级的开源分布式文件系统 FastDFS主要解决了大容量的文件存储和高并发访问的问题,文件存取时实现了负载均衡 Fas ...
- SQL Server 批量主分区备份(Multiple Jobs)
一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 案例分析(Case) 方案一(Solution One) 方案二(Solution Two) ...
- Livecoding.tv2.5发布,增加“用户搜索引擎”功能,方便用户找到匹配的程序员
近日,在Livecoding.tv最新发布的博客中,介绍了该平台2.5版的一系列新功能,其中的User Discovery Engine(用户搜索引擎)受到大家的欢迎.使用该引擎,可以很方便地查找在L ...
- 慕课网H5圣诞主题
继七夕之后,我又出了一个圣诞主题的课程.圣诞主题是基于HTML5+CSS+JS编写与实现的,同时也是七夕主题的故事延续.圣诞主题依旧延续着七夕主题设计的思路,引入了3个经典的场景页面,在每个场景中表述 ...
- Android线程管理之ExecutorService线程池
前言: 上篇学习了线程Thread的使用,今天来学习一下线程池ExecutorService. 线程管理相关文章地址: Android线程管理之Thread使用总结 Android线程管理之Execu ...
- ERROR 1010 (HY000): Error dropping database (can't rmdir './test/', errno: 17)
在删除数据库的时候报标题所示错误 mysql> drop database test; ERROR (HY000): Error dropping database (can't rmdir ' ...