• 可以直接copy运行研究

  • .m头文件和声明的常量(宏和const)

#import "ViewController.h"

// UIScrollView的尺寸

const CGFloat WSCROLL = 300;

const CGFloat HSCROLL = 200;

#define VWSCROLL (self.view.frame.size.width-WSCROLL)/2

#define VHSCROLL self.view.frame.size.height/7

// 图片的数量

#define COUNTImage 6


  • 所有的属性都封装在延展内:保证了封装性,只是给外界提供了数据传输接口,图片的显示和数据源是分开的,因此只是本类使用到的文件可以写在延展内

@interface ViewController ()

// 视图

@property (nonatomic, strong)UIScrollView *scrol;

// 翻页

@property (nonatomic, strong)UIPageControl *page;

// 固定三个视图:每次都显示中间(偏移量为当前UIScrollView的宽度:数组索引为1)

@property (nonatomic, strong)UIImageView *imv_1;

@property (nonatomic, strong)UIImageView *imv_2;

@property (nonatomic, strong)UIImageView *imv_3;

// 数据源:让显示和数据分开

@property (nonatomic, strong)NSMutableArray *arrImages;

// 记录偏移量:判断偏移后显示的图片,即数组的下标

@property (nonatomic, assign)NSInteger currentPage;

@end


  • 具体实现和注释已经很清晰了,多看

/**

*  通过偏移量,只是改变了数组的下标(换图),view还是居中

*/

@implementation ViewController

- (void)viewDidLoad

{

[super viewDidLoad];

// 1.获取数据源:让显示和数据隔离

self.arrImages = [NSMutableArray array];

for (int i = 0; i < COUNTImage; i++)

{

[self.arrImages addObject:[NSString stringWithFormat:@"bailidujuan%i.jpg", i]];

}

// 2.创建UIScrollView

self.scrol = [[UIScrollView alloc] initWithFrame:CGRectMake(VWSCROLL, VHSCROLL, WSCROLL, HSCROLL)];

// - 只是通过偏移量还更换数组内图片,3个视图保证了往左或者往右都会偏移量:减少内存占用

self.scrol.contentSize = CGSizeMake(WSCROLL*3, HSCROLL);

// - 允许分页

self.scrol.pagingEnabled = YES;

// - 隐藏滚动条

self.scrol.showsHorizontalScrollIndicator = NO;

// - 去掉回弹效果

self.scrol.bounces = NO;

// - 创建视图:因为只有三个视图,因此偏移量是两个定值:只是初始化对象

self.imv_1 = [[UIImageView alloc] init];

self.imv_2 = [[UIImageView alloc] init];

self.imv_3 = [[UIImageView alloc] init];

// 3.添加分页标识

self.page = [[UIPageControl alloc] initWithFrame:CGRectMake((self.view.frame.size.width-100)/2, WSCROLL-30, 100, 30)];

// - 确定页数

self.page.numberOfPages = COUNTImage;

// - 清除背景色

//self.page.backgroundColor = [UIColor clearColor];

// - 更换分页颜色

self.page.currentPageIndicatorTintColor = [UIColor purpleColor];

self.page.pageIndicatorTintColor = [UIColor whiteColor]; //切换页

// - 和用户不交互

self.page.userInteractionEnabled = NO;

// - 传递页数需要代理实现

self.scrol.delegate = self;

// - 当前页码

self.currentPage = 0;

// - 默认加载第一张图

[self reloadImage];

// - 添加到视图:先添加UIScrollView再添加UIPageControl,避免分页被覆盖

[self.view addSubview:self.scrol];

[self.view addSubview:self.page];

}


  • 自定义方法只是通过传递下标确定显示的图片

-(void)reloadImage

{

// 根据下标显示图片:每次都显示中间(偏移量为当前UIScrollView的宽度:数组索引三种情况)

// 每次只是在显示对应索引以及它前后的图片:索引的获取需要偏移量contentOffset

// 第一页:

if (!_currentPage)

{

self.imv_1.image = [UIImage imageNamed:[self.arrImages lastObject]];

self.imv_2.image = [UIImage imageNamed:self.arrImages[self.currentPage]];

self.imv_3.image = [UIImage imageNamed:self.arrImages[self.currentPage+1]];

}

// 最后一页

else if (_currentPage == COUNTImage -1)

{

self.imv_1.image = [UIImage imageNamed:self.arrImages[self.currentPage-1]];

self.imv_2.image = [UIImage imageNamed:self.arrImages[self.currentPage]];

self.imv_3.image = [UIImage imageNamed:[self.arrImages firstObject]];

}

// 中间页

else

{

self.imv_1.image = [UIImage imageNamed:self.arrImages[self.currentPage-1]];

self.imv_2.image = [UIImage imageNamed:self.arrImages[self.currentPage]];

self.imv_3.image = [UIImage imageNamed:self.arrImages[self.currentPage+1]];

}

// 每次修改frame:显示数据不为空再给frame赋值

self.imv_1.frame = CGRectMake(0, 0, WSCROLL, HSCROLL);

self.imv_2.frame = CGRectMake(WSCROLL, 0, WSCROLL, HSCROLL);

self.imv_3.frame = CGRectMake(WSCROLL*2, 0, WSCROLL, HSCROLL);

[self.scrol addSubview:self.imv_1];

[self.scrol addSubview:self.imv_2];

[self.scrol addSubview:self.imv_3];

// 让每次切换都能看到三张图:保证流畅性(每次显示的都是中间一张图)

self.scrol.contentOffset = CGPointMake(WSCROLL, 0);

}


  • 确定下标的过程

#pragma mark - ScrollView Delegate

//在滚动结束状态换图:DidEnd

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

// 确定下标:通过判断偏移量改变索引

// 因为只有三个视图,因此偏移量是两个定值

// 向左:偏移量恒为0

if (!self.scrol.contentOffset.x)

{

if (!_currentPage)

{

// 在第0页左移后,集合索引为最后一个对象:

_currentPage = COUNTImage - 1; // self.arrImages.count - 1 = COUNTImage - 1

}

else

{

_currentPage--; // 一直左移,只会走一次if语句直到0

}

}

// 向右:偏移量恒为两倍

if (self.scrol.contentOffset.x == WSCROLL*2)

{

if (_currentPage == COUNTImage - 1)

{

_currentPage = 0; // 在最后一页右移:集合索引就赋值为0

}

else

{

// 一直右移,只会在到达最后一个元素位置走if语句

_currentPage++;

}

}

// 页数即为集合的下标

self.page.currentPage = _currentPage;

// 对应方法显示下标对应的图片

[self reloadImage];

}


  • Xcode自带程序

#pragma mark -

- (void)didReceiveMemoryWarning

{

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end


初学者整理,思路可能很简单,希望大家可以相互学习

iOS完美版的UIScrollView无缝循环:你值得一看的更多相关文章

  1. Safari 前端开发调试 iOS 完美解决方案

    转http://www.2cto.com/kf/201403/283404.html afari 前端开发调试 iOS 完美解决方案 2014-03-05      0个评论    来源:Safari ...

  2. JavaScript学习笔记——简单无缝循环滚动展示图片的实现

    今天做了一个简单的无缝循环滚动的实例,这种实例在网页中其实还挺常见的,下面分享一下我的学习收获. 首先,无缝滚动的第一个重点就是——动.关于怎么让页面的元素节点动起来,这就得学明白关于JavaScri ...

  3. Expression Blend4经验分享:文字公告无缝循环滚动效果

    这次分享一个类似新闻公告板的无缝循环滚动效果,相信很多项目都会应用到这个效果.之前我也百度了一下,网上的一些Silverlight的文字或图片滚动效果,都是一次性滚动的,如果要做到无缝循环滚动,多数要 ...

  4. unity3d 音频无缝循环

    在我做赛车漂移的时候,漂移的声音断断续续的,搞得我很郁闷 大家可以随便找个音效然后循环播放去仔细听 你会发现当音效播放完成一次之后循环播放第二次时会停顿一下 我做赛车漂移如果中途停顿了体验是非常不好的 ...

  5. iOS开发UI篇—UIScrollView控件实现图片缩放功能

    iOS开发UI篇—UIScrollView控件实现图片缩放功能 一.缩放 1.简单说明: 有些时候,我们可能要对某些内容进行手势缩放,如下图所示 UIScrollView不仅能滚动显示大量内容,还能对 ...

  6. iOS开发UI篇—UIScrollView控件介绍

    iOS开发UI篇—UIScrollView控件介绍 一.知识点简单介绍 1.UIScrollView控件是什么? (1)移动设备的屏幕⼤大⼩小是极其有限的,因此直接展⽰示在⽤用户眼前的内容也相当有限 ...

  7. iOS开发UI篇—UIScrollView控件实现图片轮播

    iOS开发UI篇—UIScrollView控件实现图片轮播 一.实现效果 实现图片的自动轮播            二.实现代码 storyboard中布局 代码: #import "YYV ...

  8. 【转】 iOS开发UI篇—UIScrollView控件实现图片轮播

    原文:http://www.cnblogs.com/wendingding/p/3763527.html iOS开发UI篇—UIScrollView控件实现图片轮播 一.实现效果 实现图片的自动轮播 ...

  9. HTML多图无缝循环翻页效果

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

随机推荐

  1. RichTextEditor 文本编辑

    <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="ht ...

  2. storm serializable

    http://www.programgo.com/article/68283366567/

  3. MyBatis-配置缓存

    <cache type="org.mybatis.caches.ehcache.LoggingEhcache"> <property name="tim ...

  4. Number Sequence HDU 1711 KMP 模板

    题目大意:两个数组匹配,求子串首次出现的位置. 题目思路:数组长度,比较大,朴素算法的时间复杂度为 m*n超时.KMP的时间复杂度为m+n可行. #include<iostream> #i ...

  5. 推荐 iOS 网站:

    0. https://developer.apple.com/videos/1. http://www.raywenderlich.com2. http://nshipster.com3. http: ...

  6. poj 2594 Treasure Exploration(最小路径覆盖,可重点)

    题意:选出最小路径覆盖图中所有点,路径可以交叉,也就是允许路径有重复的点. 分析:这个题的难点在于如何解决有重复点的问题-方法就是使用Floyd求闭包,就是把间接相连的点直接连上边,然后就是求最小路径 ...

  7. 任意2个io直接驱动LCD1602,并且不需外加芯片(转)

    http://www.amobbs.com/thread-4301955-1-1.html *此处只摘录部分内容,详细内容请关注原贴. 这就是电路,细心的朋友会发现实物图中有几个贴片的阻容件,秘密就在 ...

  8. html-关于IE浏览器兼容性的问题,还有浏览器一直加载的问题。

    今天9月11日,所谓兼容性问题就是,标签 写的不规范导致的,因为高版本的IE会自动补上没写的标签 document.write('<object classid="clsid:d27c ...

  9. java dom4j解析xml实例

    java利用dom4j解析xml 需要的jar包: dom4j官方网站在 http://www.dom4j.org/ 下载dom4j-1.6.1.zip 解开后有两个包,仅操作XML文档的话把dom4 ...

  10. JfreeCHart 异常:Chart image not found

    http://bbs.justep.com/thread-54775-1-1.html java.lang.IllegalArgumentException: Width (0) and height ...