#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现实自动循环滚动的更多相关文章

  1. UIScrollView实现自动循环滚动广告

    实现效果如下: 功能说明: 程序运行,图片自动循环播放,采用定时器实现; 当用户用手势触摸滑动时,定时器的自动播放取消,停止触摸时,自动无限播放; 代码如下 : 采用封装视图,外部进行调用即可: 1. ...

  2. IOS实现自动循环滚动广告--ScrollView的优化和封装

    一.问题分析 在许多App中,我们都会见到循环滚动的视图,比如广告,其实想实现这个功能并不难,用ScrollView就可以轻松完成,但是在制作的过程中还存在几个小问题,如果能够正确的处理好这些小问题, ...

  3. 使用Recyclerview实现图片水平自动循环滚动

    简介: 本篇博客主要介绍的是如何使用RecyclerView实现图片水平方向自动循环(跑马灯效果) 效果图: 思路: 1.准备m张图片 1.使用Recyclerview实现,返回无数个(实际Inter ...

  4. 自动循环滚动ScrollView

    // // SBCycleScrollView.h // SBCycleScrollView // // Created by luo.h on 15/7/12. // Copyright (c) 2 ...

  5. ListView的自动循环滚动显示

    最近项目里需要做评价内容的循环滚动显示,一开始想到的就是定时器.后来查了资料才知道ListView里面有个函数smoothScrollToPosition(position),瞬间觉得简单了很多.首先 ...

  6. item上下自动循环滚动显示

    //li 上下滚动 (function($){ $.fn.extend({ Scroll:function(opt,callback){ //参数初始化 if(!opt) var opt={}; va ...

  7. IOS无限自动循环滚动banner(源码)

    本文转载至 http://blog.csdn.net/iunion/article/details/19080259  目前有很多APP都开始使用一些滚动banner,我自己也做了一个,部分算法没有深 ...

  8. UIScrollView循环滚动1

    现在基本每一个商业APP都会有循环滚动视图,放一些轮播广告之类的,都是放在UIScrollView之上.假如我要实现N张图片的轮播,我借鉴了几个博文,得到两种方法实现: [第一种]:如下图(图片来源于 ...

  9. 使用UIScrollView 结合 UIImageView 实现图片循环滚动

    场景: 在开发工作中,有时我们需要实现一组图片循环滚动的情况.当我们使用 UIScrollView 结合 UIImageView 来实现时,一般 UIImageView 会尽量考虑重用,下面例子是以( ...

随机推荐

  1. PCA算法详解——本质上就是投影后使得数据尽可能分散(方差最大),PCA可以被定义为数据在低维线性空间上的正交投影,这个线性空间被称为主⼦空间(principal subspace),使得投影数据的⽅差被最⼤化(Hotelling, 1933),即最大方差理论。

    PCA PCA(Principal Component Analysis,主成分分析)是一种常用的数据分析方法.PCA通过线性变换将原始数据变换为一组各维度线性无关的表示,可用于提取数据的主要特征分量 ...

  2. HihoCoder 1190连通性·四

    连通性·四 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho从约翰家回到学校时,网络所的老师又找到了小Hi和小Ho. 老师告诉小Hi和小Ho:之前的分组出了 ...

  3. uoj#87. mx的仙人掌

    //Achen #include<bits/stdc++.h> #define For(i,a,b) for(int i=(a);i<=(b);i++) #define Rep(i, ...

  4. GO语言list剖析

    GO语言list剖析 本节内容 使用方法 list提供的方法 源码剖析 1. 使用方法 在GO语言的标准库中,提供了一个container包,这个包中提供了三种数据类型,就是heap,list和rin ...

  5. Android Studio导入项目,报错 Error:Unsupported method: BaseConfig.getApplicationIdSuffix().

    从GitHub上clone下来的第三方库,由于时间间隔很长,gradle的版本和本机的版本不一致,导入到Android Studio中会报错,错误信息如下: Error:Unsupported met ...

  6. BZOJ2724:[Violet 6]蒲公英

    浅谈分块:https://www.cnblogs.com/AKMer/p/10369816.html 题目传送门:https://lydsy.com/JudgeOnline/problem.php?i ...

  7. .NET Framework、C#、CLR和Visual Studo之间的版本关系

    .NET Framework.C#.CLR和Visual Studo之间的版本关系 参考 .NET Framework.C#.CLR和Visual Studo之间的版本关系

  8. 如何禁用 a 标签的点击事件

    a标签是没有disable属性的 ,如果想用disable 禁用a标签的点击事件,也可以实现: 1.a标签要用disable属性,必须和pointer-events属性一起使用, html代码: &l ...

  9. POJ1159:动态规划

    Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 60290   Accepted: 20998 Desc ...

  10. Windows 7 下将 Tomcat Java 程序设置为 Windows Service

    方法: Windows key + r -> Run dialog cmd -> console cd apache-tomcat-[version]/bin service.bat in ...