#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. 解密jQuery事件核心 - 自定义设计(三)

    接上文http://www.cnblogs.com/aaronjs/p/3447483.html 本文重点:自定义事件 “通过事件机制,可以将类设计为独立的模块,通过事件对外通信,提高了程序的开发效率 ...

  2. lua中的数据类型

    lobject.h: lobject.h: 其中使用GCObject表示的数据类型是需要lua 的gc记录的. lstate.h: lobject.h:

  3. (转)J2EE的13种核心技术

    一.JDBC(Java Database Connectivity)  JDBC API为访问不同的数据库提供了一种统一的途径,象ODBC一样,JDBC对开发者屏蔽了一些细节问题,另外,JDBC对数据 ...

  4. 简述ASP.NET MVC原理

    1.为什么ASP.NET需要MVC? 因为随着网站的的数量级越来越大,原始的网站方式,这里指的是WebForm,在运行速度和维护性方面,以及代码量上面,越来越难以满足日益庞大的网站维护成本.代码的重构 ...

  5. php学习零散笔记—字符串分割、fetch函数和单双引号。

    1 字符串分割——split()函数和preg_split()函数 split — 用正则表达式将字符串分割到数组中——貌似PHP5.3以上已不赞成使用 array split ( string $p ...

  6. sqlite - java 初学

    进来准备使用一种embedded database,即嵌入式数据库,方便随项目本地存储.目前学习打算是sqlite和H2. document:http://www.runoob.com/sqlite/ ...

  7. Net设计模式实例之简单工厂模式(Simple Factory Pattern)

    一.简单工厂模式简介(Bref Introduction) 简单工厂模式(Simple Factory Pattern)的优点是,工厂类中包含了必要的逻辑判断,根据客户端的选择条件动态实例化相关的类, ...

  8. Python字典实现分析

    背景介绍 最近使用Python开发项目为主,当使用到字典时感觉非常方便实用.那么好奇心就驱使我要搞清楚字典是怎么实现的.为了真正的搞清楚字典的实现就不得不使用C语言来实现一遍,为此我查了一些资料现在总 ...

  9. Python_Day_02 str内部方法总结

    刚开始学习Python,看了一天的字符串内部方法,现在来总结一下. capitalize(self) 将一句话的首字母变大写,其他字母都变小 name = "love PyThon" ...

  10. Xamarin.Android之布局文件智能提示问题

    一.前言 看到有人问关于xamarin.android的布局没智能提示问题(VS 2015),当然,写布局这东西没提示这是一件相对痛苦的事 ,所以这里就提供一个解决的方案! 二.解决方案 想要智能提示 ...