UIScrollView现实循环滚动
#import "RootViewController.h" #define width [UIScreen mainScreen].bounds.size.width
#define heigthY 150 @interface RootViewController ()<UIScrollViewDelegate>
{
UIScrollView *_scrollView;
NSMutableArray *imageArray;
UIPageControl *pageControl;
}
@end @implementation RootViewController - (void)dealloc
{
imageArray = nil;
[super dealloc];
} - (void)loadView
{
[super loadView];
_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(, , width, heigthY)];
_scrollView.pagingEnabled = YES;
_scrollView.delegate = self;
// 开始时选中第二个图片 图片的布局[3-1-2-3-1]
_scrollView.contentOffset = CGPointMake(width, );
// 隐藏水平滚动条
_scrollView.showsHorizontalScrollIndicator = NO; pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(width - ,heigthY - , , )];
// 设置pageControl不支持用户操作
pageControl.userInteractionEnabled = NO;
pageControl.currentPageIndicatorTintColor = [UIColor redColor];
pageControl.pageIndicatorTintColor = [UIColor greenColor];
[self.view addSubview:_scrollView];
[self.view addSubview:pageControl];
[_scrollView release];
[pageControl release]; } - (void)viewDidLoad {
[super viewDidLoad];
imageArray = [[NSMutableArray alloc] init];
NSArray *tempArray = @[@"1-3.jpg",@"1-1.jpg",@"1-2.jpg",@"1-3.jpg",@"1-1.jpg"];
[imageArray addObjectsFromArray:tempArray];
// 根据imageArray的数量设置_scrollView的内容大小
_scrollView.contentSize = CGSizeMake(width * imageArray.count, heigthY);
pageControl.numberOfPages = imageArray.count - ;
// 给_scrollView添加图片
[self addImagesWithScrollView];
} /**
* 给scrollView添加图片
*/
- (void)addImagesWithScrollView
{
for (int i = ; i < imageArray.count; i++) {
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageArray[i]]];
imageView.frame = CGRectMake(i * width, , width, heigthY);
[_scrollView addSubview:imageView];
[imageView release];
}
} #pragma mark - UIScrollViewDelegate的方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
int imageIndex = scrollView.contentOffset.x / width;
if (imageIndex == ) {
// 滚到第一张图片时,就跳转到倒数第二张图片
[_scrollView scrollRectToVisible:CGRectMake((imageArray.count - )*width, , width, heigthY) animated:NO];
}else if (imageIndex == imageArray.count - ){
// 滚动到最后一张图片时,就跳转到第二张图片
[_scrollView scrollRectToVisible:CGRectMake(width, , width, heigthY) animated:NO];
}
} /**
* 设置pageControl的当前页
*/
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
// 加0.5是为了用户体验好些,滑动过程中哪张图片占优就显示占优图片对应的下标
int imageIndex = scrollView.contentOffset.x / width + 0.5;
if (imageIndex == ) {
// 设置相应的下标(使之减1后与pageControl的下标相对应)
imageIndex = imageArray.count - ;
}else if (imageIndex == imageArray.count - ){
// 设置相应的下标(使之减1后与pageControl的下标相对应)
imageIndex = ;
}
pageControl.currentPage = imageIndex - ;
}
@end
UIScrollView现实循环滚动的更多相关文章
- UIScrollView 图片循环滚动
1:假如有6个图片:那个,Scrollview的大小加 7 个图片的大小 2: //ImageScrollView; UIScrollView *imageScroll = [[UIScrollVie ...
- UIScrollView循环滚动1
现在基本每一个商业APP都会有循环滚动视图,放一些轮播广告之类的,都是放在UIScrollView之上.假如我要实现N张图片的轮播,我借鉴了几个博文,得到两种方法实现: [第一种]:如下图(图片来源于 ...
- 使用UIScrollView 结合 UIImageView 实现图片循环滚动
场景: 在开发工作中,有时我们需要实现一组图片循环滚动的情况.当我们使用 UIScrollView 结合 UIImageView 来实现时,一般 UIImageView 会尽量考虑重用,下面例子是以( ...
- UIScrollView 循环滚动,代码超简单
如今非常多应用里面多多少少都用到了循环滚动,要么是图片.要么是view,或者是其它,我总结一下,写了个demo分享给大家. 先看代码之后在讲原理: 1.创建一个空的项目(这个我就不多说了). 2.加入 ...
- UIScrollView实现自动循环滚动广告
实现效果如下: 功能说明: 程序运行,图片自动循环播放,采用定时器实现; 当用户用手势触摸滑动时,定时器的自动播放取消,停止触摸时,自动无限播放; 代码如下 : 采用封装视图,外部进行调用即可: 1. ...
- IOS实现自动循环滚动广告--ScrollView的优化和封装
一.问题分析 在许多App中,我们都会见到循环滚动的视图,比如广告,其实想实现这个功能并不难,用ScrollView就可以轻松完成,但是在制作的过程中还存在几个小问题,如果能够正确的处理好这些小问题, ...
- NGUI实现的一套不同大小 Item 的循环滚动代码
测试: 数据 & Item 的 Ctrl : using UnityEngine; public class ScrollViewItemData { public int index; p ...
- Jquery制作--循环滚动列表
自己模仿JQ插件的写法写了一个循环滚动列表插件,支持自定义上.下.左.右四个方向,支持平滑滚动或者间断滚动两种方式,都是通过参数设置.JQ里面有些重复的地方,暂时没想到更好的方法去精简.不过效果还是可 ...
- marquee 实现首尾相连循环滚动效果
<marquee></marquee>可以实现多种滚动效果,无需js控制.使用marquee标签不仅可以滚动文字,也可以滚动图片,表格等 marquee标签不是HTML3.2 ...
随机推荐
- SCM-MANAGER 应用
什么是SCM-MANAGER 基于Web的,集成了 Git. Mercurial .Subversion 多种代码管理工具的源代码管理平台 它有什么优点 简易安装 不需要破解配置文件,完全可配置的 ...
- CodeForces - 13D :Triangles(向量法:问多少个蓝点三角形内部无红点)
Little Petya likes to draw. He drew N red and M blue points on the plane in such a way that no three ...
- CodeSmith 基本语法(二)
CodeSmith之四 - 典型实例(四) CodeSmith API文档 (三) CodeSmith 基本语法(二) CodeSmith 图形界面基本操作(一) CodeSmith的C#语法与Asp ...
- 接口方式[推荐]/动态SQL语句
MVC目录结构: Src -- com.shxt.servlet[控制层] --com.shxt.service[业务逻辑层] --com.shxt.model[实体Bean,用来承载数据] --co ...
- [C++] 分治法之棋盘覆盖、循环赛日程表
一.分治的基本思想 将一个难以直接解决的大问题,分割成一些规模较小的相同问题,以便各个击破,分而治之. 对于一个规模为 n 的问题,若问题可以容易地解决,则直接解决,否则将其分解为 k 个规模较小的子 ...
- 蓝桥杯 历届试题 PREV-33 兰顿蚂蚁
历届试题 兰顿蚂蚁 时间限制:1.0s 内存限制:256.0MB 问题描述 兰顿蚂蚁,是于1986年,由克里斯·兰顿提出来的,属于细胞自动机的一种. 平面上的正方形格子被填上黑色或白色.在其中 ...
- HDU1695(容斥原理)
GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- windows任务管理器中的工作设置内存,内存专用工作集,提交大小详解
虽然是中文字,但是理解起来还是很困难,什么叫工作设置内存,什么叫内存专用工作集,什么叫提交大小,区别是什么,让人看了一头雾水. 通俗的讲工作设置内存是程序占用的物理内存(包含与其他程序共享的一部分), ...
- C# Lambda快速深度拷贝
背景:今天上班在班车上和一个同事讨论有关C#拷贝效率的问题,聊到了多种深度拷贝方法,其中就提到了一种Lambda表达式拷贝的方法,这位同事说这种深度拷贝快是快但是如果对象里面再嵌入对象就不能深度拷贝了 ...
- BIOS简单设置 解析“集成显卡”内存占用问题
很多使用集成显卡的用户会发现,在系统信息窗口中,内存容量和实际不一样.比如系统内存显示4GB,可用3.48G之类.这不可用的一部分内存到哪去了? 其实减少的这部分内存是被集成显卡占用当做显存使用了.而 ...