#import "ViewController.h"

#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width

#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

#define IMAGEVIEW_COUNT 3

@interface ViewController () <UIScrollViewDelegate> {

UIScrollView *_scrollView;

UIImageView *_leftImageView;

UIImageView *_centerImageView;

UIImageView *_rightImageView;

UIPageControl *_pageControl;

UILabel *_label;

NSMutableDictionary *_imageData;//图片数据

int _currentImageIndex;//当前图片索引

int _imageCount;//图片总数

}

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

//加载数据

[self loadImageData];

//添加滚动控件

[self addScrollView];

//添加图片控件

[self addImageViews];

//添加分页控件

[self addPageControl];

//添加图片信息描述控件

[self addLabel];

//加载默认图片

[self setDefaultImage];

}

#pragma mark 加载图片数据

-(void)loadImageData {

//读取程序包路径中的资源文件

NSString *path = [[NSBundle mainBundle] pathForResource:@"imageInfo" ofType:@"plist"];

_imageData = [NSMutableDictionary dictionaryWithContentsOfFile:path];

_imageCount = (int)_imageData.count;

}

#pragma mark 添加控件

-(void)addScrollView {

//    _scrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];

_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 20+20, SCREEN_WIDTH, 200)];

_scrollView.backgroundColor = [UIColor lightGrayColor];

//设置代理

_scrollView.delegate = self;

//设置滚动范围

_scrollView.contentSize = CGSizeMake(IMAGEVIEW_COUNT*SCREEN_WIDTH, 0);

//设置当前显示的位置为中间图片(设置scrollView偏移量)

[_scrollView setContentOffset:CGPointMake(SCREEN_WIDTH, 0)];

//设置分页

_scrollView.pagingEnabled = YES;

//隐藏水平条 竖向条

_scrollView.showsHorizontalScrollIndicator = NO;

_scrollView.showsVerticalScrollIndicator = NO;

//关闭弹簧效果

//    _scrollView.bounces = NO;

[self.view addSubview:_scrollView];

}

#pragma mark 添加图片三个控件

-(void)addImageViews {

_leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, _scrollView.frame.size.height)];

//会保证图片比例不变,而且全部显示在ImageView中,这意味着ImageView会有部分空白

_leftImageView.contentMode=UIViewContentModeScaleAspectFit;

[_scrollView addSubview:_leftImageView];

_centerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, _scrollView.frame.size.height)];

_centerImageView.contentMode = UIViewContentModeScaleAspectFit;

[_scrollView addSubview:_centerImageView];

_rightImageView = [[UIImageView alloc] initWithFrame:CGRectMake(2*SCREEN_WIDTH, 0, SCREEN_WIDTH, _scrollView.frame.size.height)];

_rightImageView.contentMode = UIViewContentModeScaleAspectFit;

[_scrollView addSubview:_rightImageView];

}

#pragma mark 设置默认显示图片

-(void)setDefaultImage {

//加载默认图片

_leftImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%i.jpeg",_imageCount-1]];

_centerImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%i.jpeg",0]];

_rightImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%i.jpeg",1]];

_currentImageIndex = 0;

//设置当前页

_pageControl.currentPage = _currentImageIndex;

NSString *imageName = [NSString stringWithFormat:@"%i.jpeg",_currentImageIndex];

_label.text = _imageData[imageName];

}

#pragma mark 添加分页控件

-(void)addPageControl {

_pageControl = [[UIPageControl alloc] init];

//    _pageControl.backgroundColor = [UIColor orangeColor];

//注意此方法可以根据页数返回UIPageControl合适的大小

CGSize size = [_pageControl sizeForNumberOfPages:_imageCount];

_pageControl.bounds = CGRectMake(0, 0, size.width, size.height);

_pageControl.center = CGPointMake(SCREEN_WIDTH*2/3, _scrollView.frame.origin.y+_scrollView.frame.size.height-10);

//设置颜色

_pageControl.pageIndicatorTintColor = [UIColor colorWithRed:193/255.0 green:219/255.0 blue:249/255.0 alpha:1];

//设置当前页颜色

_pageControl.currentPageIndicatorTintColor = [UIColor colorWithRed:0 green:150/255.0 blue:1 alpha:1];

//设置总页数

_pageControl.numberOfPages = _imageCount;

[self.view addSubview:_pageControl];

}

#pragma mark 添加信息描述控件

-(void)addLabel {

_label = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, SCREEN_WIDTH, 20)];

//    _label.backgroundColor = [UIColor orangeColor];

_label.textAlignment = NSTextAlignmentCenter;

_label.textColor = [UIColor colorWithRed:0 green:150/255.0 blue:1 alpha:1];

[self.view addSubview:_label];

}

#pragma mark 滚动停止事件

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

//重新加载图片

[self reloadImage];

//移动到中间

[_scrollView setContentOffset:CGPointMake(SCREEN_WIDTH, 0)];

//设置分页

_pageControl.currentPage = _currentImageIndex;

//设置描述

NSString *imageName = [NSString stringWithFormat:@"%i.jpeg",_currentImageIndex];

_label.text = _imageData[imageName];

}

#pragma mark 重新加载图片

-(void)reloadImage {

int leftImageIndex,rightImageIndex;

CGPoint offset = [_scrollView contentOffset];

if (offset.x > SCREEN_WIDTH) { //向右滑动

_currentImageIndex = (_currentImageIndex+1)%_imageCount;

}else if(offset.x < SCREEN_WIDTH) { //向左滑动

_currentImageIndex = (_currentImageIndex+_imageCount-1)%_imageCount;

}

_centerImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%i.jpeg",_currentImageIndex]];

//重新设置左右图片

leftImageIndex = (_currentImageIndex+_imageCount-1)%_imageCount;

rightImageIndex = (_currentImageIndex+1)%_imageCount;

_leftImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%i.jpeg",leftImageIndex]];

_rightImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%i.jpeg",rightImageIndex]];

}

@end

iOS之UIScrollView循环滚动的更多相关文章

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

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

  2. UIScrollView循环滚动1

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

  3. UIScrollView 循环滚动,代码超简单

    如今非常多应用里面多多少少都用到了循环滚动,要么是图片.要么是view,或者是其它,我总结一下,写了个demo分享给大家. 先看代码之后在讲原理: 1.创建一个空的项目(这个我就不多说了). 2.加入 ...

  4. iOS判断UIScrollView的滚动方向

    - (void) scrollViewDidScroll:(UIScrollView *)scrollView { CGFloat newY = scrollView.contentOffset.y; ...

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

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

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

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

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

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

  8. ios UIPickerView 技巧集锦(包括循环滚动)

    摘自: http://blog.csdn.net/ipromiseu/article/details/7436521 http://www.cnblogs.com/dabaopku/archive/2 ...

  9. Cocos2dx中利用双向链表实现无限循环滚动层

    [Qboy原创] 在Cocos2dX 3.0 中已经实现一些牛逼的滚动层,但是对于有一些需要实现循环滚动的要求确没有实现,笔者在前段时间的一个做了一个游戏,需求是实现在少有的(13个)英雄中进行循环滚 ...

随机推荐

  1. Activity四种启动模式

    转载博客:http://blog.csdn.net/shinay/article/details/7898492 Activity启动方式有四种,分别是:standardsingleTopsingle ...

  2. .Net 转战 Android 4.4 日常笔记(9)--常用组件的使用方法[附源码]

    经过两天的学习,把常用的组件都学习了一遍,并做成了App 学习可能真没有捷径,跟学习html有点类似,都是一个控件一个控件学习并使用,最后拼凑成一个系统 链接:http://pan.baidu.com ...

  3. 配置 linux-bridge mechanism driver - 每天5分钟玩转 OpenStack(77)

    本节开始我们将学习 Linux Bridge 如何实现 Neutron 的各种功能.首先需要配置 linux-bridge mechanism driver. Neutorn ML2 plugin 默 ...

  4. javascript之一切皆为对象2

    其实呢,“函数function”和“对象object”之间还有这么一句话:对象是通过函数来创建的,而函数却又是一种对象. 这个函数是一种对象,上节中“Javascript之一切皆为对象1”也清楚的阐述 ...

  5. ZOJ Problem Set - 1109 Language of FatMouse

    这道题目最让人头疼的就是该题的input怎么结束,因为它要求输入一个空行的时候则一串字符串输入结束,这就不得不让人绕个弯来解决这个问题. (注:本人习惯于使用C中的字符串操作,但是用到map要求使用s ...

  6. RichTextBox实现鼠标右键(剪切,复制,粘贴)功能

    private static void InitRichTextBoxContextMenu(RichTextBox textBox) { //创建剪切子菜单 var cutMenuItem = ne ...

  7. struct 大小计算

    结构体是一种复合数据类型,通常编译器会自动的进行其成员变量的对齐,已提高数据存取的效率.在默认情况下,编译器为结构体的成员按照自然对齐(natural alignment)条方式分配存储空间,各个成员 ...

  8. idea 自动提示生成 serialVersionUID

    from: http://tonycody.blog.51cto.com/8421818/1401422 Intellij IDEA 默认没启用这个功能. Setting->Inspection ...

  9. 使用fiddler的autoResponder及设置手机端代理实现远程调试,出现的问题及解决办法

    这是开通博客的第一篇随笔,好鸡冻哈哈o_O 首先是下载安装,我安装的是最新的v4.6.2.0版本,大家在百度上搜fidddler4在百度软件中心普通下载就可以了.或者直接用这个连接:http://dl ...

  10. .net程序部署(mono方式)

    某一次 我同事用了这个词 ,说这样才显得够专业 擦.把某某项目 部署到服务器上 .擦 不就是拷个文件过去运行么.月亮 还是绵羊  我搞不清楚了 咱英文不好,绵羊叫的声音?.你就叫我山寨程序猿 随意 一 ...