关于UIScrollView图片浏览的例子有很多,之前也写过类似方面的文章,关于UIScrollView的图片循环在新闻类的App基本上是比较常见的一种情况就是图片浏览,然后根据不同的图片显示不同的内容显示不同的图片的介绍,因为属于比较常用的空间,先来看下需要实现的效果:

小圆点指示器是通过UIPageControl实现的,图片循环通过UIScrollView实现:

-(UIPageControl *)pageControl{
if (!_pageControl) {
_pageControl=[[UIPageControl alloc]initWithFrame:CGRectMake(0, 0, 150, 40)];
_pageControl.currentPage=0;
_pageControl.pageIndicatorTintColor=[UIColor whiteColor];
_pageControl.currentPageIndicatorTintColor=[UIColor greenColor];
}
return _pageControl;
}
-(UIScrollView *)scrollView{
if (!_scrollView) {
CGRect screenRect=[[UIScreen mainScreen]bounds];
_scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, CGRectGetWidth(screenRect), CGRectGetHeight(screenRect)-164)];
[_scrollView setBounces:NO];
[_scrollView setShowsHorizontalScrollIndicator:NO];
[_scrollView setPagingEnabled:YES];
_scrollView.delegate=self;
}
return _scrollView;
}

关于分页的页面配置以及页数Label的设置:

    self.screenRect=[[UIScreen mainScreen]bounds];
CGFloat width=self.scrollView.bounds.size.width;
CGFloat height=self.scrollView.bounds.size.height;
self.imageArr=@[@"girl0.jpg",@"girl1.jpg",@"girl2.jpg"];
[self.scrollView setContentSize:CGSizeMake([self.imageArr count]*width, height)]; for (NSInteger i=0; i<[self.imageArr count]; i++) {
UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(i*width, 0, width,height)];
imageView.image=[UIImage imageNamed:self.imageArr[i]];
imageView.contentMode=UIViewContentModeScaleToFill;
[self.scrollView addSubview:imageView];
}
[self.view addSubview:self.scrollView]; self.pageControl.numberOfPages=[self.imageArr count];
self.pageControl.center=CGPointMake(self.scrollView.center.x, CGRectGetMaxY(self.scrollView.bounds)+50);
[self.view addSubview:self.pageControl];
self.pageLabel.text=[NSString stringWithFormat:@"%d/%lu",1,[self.imageArr count]];
[self.view addSubview:self.pageLabel];

实现UIScrollViewDelegate中scrollViewDidEndDecelerating的方法:

//博客园-FlyElephant 原文地址:http://www.cnblogs.com/xiaofeixiang/
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
if (self.currentPage==[self.imageArr count]-1) {
self.currentPage=0;
self.scrollView.contentOffset=CGPointMake(0, 0);
}else{
self.currentPage=scrollView.contentOffset.x/CGRectGetWidth(self.screenRect);
}
self.pageControl.currentPage=self.currentPage;
[self.pageLabel setText:[NSString stringWithFormat:@"%ld/%lu",self.currentPage+1,[self.imageArr count]]];
}

 简单的UIScrollView实现基本上ok了,当然关于UIScrollView根据不同场景去实现不同的功能代码量比这肯定更复杂~

iOS开发-UIScrollView图片无限循环的更多相关文章

  1. iOS开发UI篇—无限轮播(循环利用)

    iOS开发UI篇—无限轮播(循环利用) 一.无限轮播  1.简单说明 在开发中常需要对广告或者是一些图片进行自动的轮播,也就是所谓的无限滚动. 在开发的时候,我们通常的做法是使用一个UIScrollV ...

  2. iOS开发UI篇—无限轮播(循环展示)

    iOS开发UI篇—无限轮播(循环展示) 一.简单说明 之前的程序还存在一个问题,那就是不能循环展示,因为plist文件中只有五个数组,因此第一个和最后一个之后就没有了,下面介绍处理这种循环展示问题的小 ...

  3. iOS开发UI篇—无限轮播(功能完善)

    iOS开发UI篇—无限轮播(功能完善) 一.自动滚动 添加并设置一个定时器,每个2.0秒,就跳转到下一条. 获取当前正在展示的位置. [self addNSTimer]; } -(void)addNS ...

  4. iOS开发UI篇—无限轮播(新闻数据展示)

    iOS开发UI篇—无限轮播(新闻数据展示) 一.实现效果        二.实现步骤 1.前期准备 (1)导入数据转模型的第三方框架MJExtension (2)向项目中添加保存有“新闻”数据的pli ...

  5. iOS开发基础-图片切换(4)之懒加载

    延续:iOS开发基础-图片切换(3),对(3)里面的代码用懒加载进行改善. 一.懒加载基本内容 懒加载(延迟加载):即在需要的时候才加载,修改属性的 getter 方法. 注意:懒加载时一定要先判断该 ...

  6. iOS开发基础-图片切换(3)之属性列表

    延续:iOS开发基础-图片切换(2),对(2)里面的代码用属性列表plist进行改善. 新建 Property List 命名为 Data 获得一个后缀为 .plist 的文件. 按如图修改刚创建的文 ...

  7. iOS开发基础-图片切换(2)之懒加载

    延续:iOS开发基础-图片切换(1),对(1)里面的代码进行改善. 在 ViewController 类中添加新的数组属性:  @property (nonatomic, strong) NSArra ...

  8. 利用jQuery实现图片无限循环轮播(不借助于轮播插件)

    原来我主要是用Bootstrap框架或者swiper插件实现轮播图的功能,而这次是用jQuery来实现图片无限循环轮播! 用到的技术有:html.css.JavaScript(少).jQuery(主要 ...

  9. iOS开发UIScrollView的底层实现

    起始 做开发也有一段时间了,经历了第一次完成项目的激动,也经历了天天调用系统的API的枯燥,于是就有了探索底层实现的想法. 关于scrollView的思考 在iOS开发中我们会大量用到scrollVi ...

随机推荐

  1. openQPA[01]初次认识与使用

    开源项目QPA 1.项目主页:[http://protocol.sinaapp.com/] 2.项目介绍: 3.运行项目: (1)安装python2.7,并安装PyQt4.   下载地址[https: ...

  2. JAVA初学练手项目,学生管理系统

    github地址:https://github.com/qscqesze/StudentManager 简单描述一下: UI层面用于接受用户的处理信息,然后移交给StudentDao去处理数据. 其中 ...

  3. 面试必会函数源代码 strcpy/memcpy/atoi/kmp/quicksort

    http://blog.csdn.net/liuqiyao_01/article/details/26967813 二.stl模板函数 1.strcpy char * strcpy( char *st ...

  4. DIV+javascript实现首尾相连循环滚动效果

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. Driving proportional valves from microcontroller

    Driving proportional valves from microcontroller I am looking to drive a current regulated proportio ...

  6. [Ubuntu] 编译安装 PHP 依赖库

    编译环境 sudo apt-get -y install build-essential xml sudo apt-get -y install libxml2-dev pcre sudo apt-g ...

  7. javascript UI框架

    http://www.jianshu.com/p/709e8d6c03c9 http://www.cnblogs.com/kingboy2008/p/5261771.html jQuery uiboo ...

  8. 自己动手写Vue插件Toast

    <style> .vue-toast { width: 100%; height: 100%; position: fixed; top: 0px; left: 0px; backgrou ...

  9. 使用RemObjects Pascal Script

    摘自RemObjects Wiki 本文提供RemObjects Pascal Script的整体概要并演示如何创建一些简单的脚本. Pascal Script包括两个不同部分: 编译器 (uPSCo ...

  10. winform 给textbox 增加 或 减小字体大小 z

    private void btnAddFont_Click(object sender, EventArgs e) { float fSize = this.txtResult.Font.Size; ...