UIScrollView现实自动循环滚动
#import "RootViewController.h" #define width [UIScreen mainScreen].bounds.size.width
#define heigthY 150
#define scrollTime 1 @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]; // 添加定时器
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:scrollTime target:self selector:@selector(automateScroll) userInfo:nil repeats:YES];
} // 每隔scrollTime秒就滚动一次
- (void)automateScroll
{
// 因为开始时出现的图片是imageArray中的第二张图片,后面求余后还加1,使得contentOffset为2*width
static int contentOffsetX = ;
// 为了偏移量始终在(1 至 imageArray.count-2)*width中,((contentOffsetX % (imageArray.count - 2))求余得到的数值在 0~imageArray.count-3中, + 1后让数值保持在 1 至 imageArray.count-2 中
_scrollView.contentOffset = CGPointMake(((contentOffsetX % (imageArray.count - )) + ) *width, );
contentOffsetX ++;
NSLog(@"==%d",contentOffsetX);
}
/**
* 给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 = (int)imageArray.count - ;
}else if (imageIndex == imageArray.count - ){
// 设置相应的下标(使之减1后与pageControl的下标相对应)
imageIndex = ;
}
pageControl.currentPage = imageIndex - ;
} @end
UIScrollView现实自动循环滚动的更多相关文章
- UIScrollView实现自动循环滚动广告
实现效果如下: 功能说明: 程序运行,图片自动循环播放,采用定时器实现; 当用户用手势触摸滑动时,定时器的自动播放取消,停止触摸时,自动无限播放; 代码如下 : 采用封装视图,外部进行调用即可: 1. ...
- IOS实现自动循环滚动广告--ScrollView的优化和封装
一.问题分析 在许多App中,我们都会见到循环滚动的视图,比如广告,其实想实现这个功能并不难,用ScrollView就可以轻松完成,但是在制作的过程中还存在几个小问题,如果能够正确的处理好这些小问题, ...
- 使用Recyclerview实现图片水平自动循环滚动
简介: 本篇博客主要介绍的是如何使用RecyclerView实现图片水平方向自动循环(跑马灯效果) 效果图: 思路: 1.准备m张图片 1.使用Recyclerview实现,返回无数个(实际Inter ...
- 自动循环滚动ScrollView
// // SBCycleScrollView.h // SBCycleScrollView // // Created by luo.h on 15/7/12. // Copyright (c) 2 ...
- ListView的自动循环滚动显示
最近项目里需要做评价内容的循环滚动显示,一开始想到的就是定时器.后来查了资料才知道ListView里面有个函数smoothScrollToPosition(position),瞬间觉得简单了很多.首先 ...
- item上下自动循环滚动显示
//li 上下滚动 (function($){ $.fn.extend({ Scroll:function(opt,callback){ //参数初始化 if(!opt) var opt={}; va ...
- IOS无限自动循环滚动banner(源码)
本文转载至 http://blog.csdn.net/iunion/article/details/19080259 目前有很多APP都开始使用一些滚动banner,我自己也做了一个,部分算法没有深 ...
- UIScrollView循环滚动1
现在基本每一个商业APP都会有循环滚动视图,放一些轮播广告之类的,都是放在UIScrollView之上.假如我要实现N张图片的轮播,我借鉴了几个博文,得到两种方法实现: [第一种]:如下图(图片来源于 ...
- 使用UIScrollView 结合 UIImageView 实现图片循环滚动
场景: 在开发工作中,有时我们需要实现一组图片循环滚动的情况.当我们使用 UIScrollView 结合 UIImageView 来实现时,一般 UIImageView 会尽量考虑重用,下面例子是以( ...
随机推荐
- 动态加载CSS,JS文件
var Head = document.getElementsByTagName('head')[0],style = document.createElement('style'); //文件全部加 ...
- LeetCode OJ:Binary Tree Paths(二叉树路径)
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- New Concept English three (48)
23w/m 76errors In this much-travelled world, there are still thousands of places which are inaccessi ...
- mac下安装libpng环境
用go写一个爬虫工具时需要使用一个go的库,而这个库有需要使用libpng库,不然编译就会提示说 png.h找不到等之类的信息,于是想到应该和windows一样需要安装gcc环境,然后让gcc里安装l ...
- 转载:关于消息队列的使用----ActiveMQ,RabbitMQ,ZeroMQ,Kafka,MetaMQ,RocketMQ
转载: http://blog.csdn.net/konglongaa/article/details/52208273
- JvisualVm添加远程监控
一.Weblogic远程监控 1.首先需要在远程的weblogic的域下面,找到/bin/ setDomainEnv.sh ,需要在此文件下加入如下内容: -Dcom.sun.management.j ...
- [Project Euler] 来做欧拉项目练习题吧: 题目013
问题描述: Work out the first ten digits of the sum of the following one-hundred 50-digit numbers. 371072 ...
- ubuntu更改用户登录密码
sudo passwd user(root或对应的用户名)
- [转载]create_proc_read_entry中函数的说明
原型: struct proc_dir_entry *create_proc_read_entry (const char *name, mode_t mode, struct proc_dir_en ...
- DotNetBar笔记
1.TextBoxDropDown 这是一个绝对TMD坑爹的狗屁玩意儿.键盘的四个事件全部不好使.但是这个玩意儿有个好处就是他的DropDownControl属性可以用来制作ComboGrid. 然 ...