//

//  AlbumViewController.m

//  HwangKop08.18

//

//  Created by rimi on 15/8/20.

//  Copyright (c) 2015年 rimi. All rights reserved.

//

#import "AlbumViewController.h"

#define IMAGE_COUNT 3

@interface AlbumViewController () <UIScrollViewDelegate>

{

NSTimer *_timer;

}

@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) UIPageControl *pageControl;

@property (nonatomic, strong) NSMutableArray *imageNameArray;

@property (nonatomic, strong) NSMutableArray *imageViewArray;

@property (nonatomic, assign) NSInteger currentIndex;

- (void)initDataSource;

- (void)initUserInterface;

@end

@implementation AlbumViewController

- (void)viewDidLoad {

[super viewDidLoad];

[self initDataSource];

[self initUserInterface];

}

- (void)initDataSource {

self.currentIndex = 0;

self.imageNameArray = [[NSMutableArray alloc] init];

self.imageViewArray = [[NSMutableArray alloc] init];

for (int i = 0; i < 8; i ++) {

NSString *name = [NSString stringWithFormat:@"%d.png", i];

[self.imageNameArray addObject:name];

}

}

- (void)initUserInterface {

//关闭自适应scrollView边界

self.automaticallyAdjustsScrollViewInsets = NO;

UIScrollView * scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];

scrollView.delegate = self;

scrollView.backgroundColor = [UIColor orangeColor];

//设置内容大小

scrollView.contentSize = CGSizeMake(IMAGE_COUNT * CGRectGetWidth(scrollView.bounds), CGRectGetHeight(scrollView.bounds));

//设置偏移量

scrollView.contentOffset = CGPointMake(CGRectGetWidth(self.view.bounds), 0);

//开启分页

scrollView.pagingEnabled = YES;

[self.view addSubview:scrollView];

self.scrollView = scrollView;

//添加子视图

for (int i = 0; i < IMAGE_COUNT; i ++) {

CGFloat width = CGRectGetWidth(self.view.bounds);

CGFloat height = CGRectGetHeight(self.view.bounds);

CGFloat x = i * width;

//创建图片视图

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(x, 0, width, height)];

[scrollView addSubview:imageView];

[self.imageViewArray addObject:imageView];

}

[self dynamicLoadImage];

#pragma mark -- UIPageControl

    //创建pageControl

    UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 200, 375, 50)];

    //设置pageControl的页数(是个小圆点)

    pageControl.numberOfPages = 8;

    //配置颜色 当前选中颜色和没有选中颜色

    pageControl.currentPageIndicatorTintColor = [UIColor orangeColor];

    pageControl.pageIndicatorTintColor = [UIColor colorWithRed:0.443 green:0.447 blue:0.435 alpha:1.000];

    pageControl.currentPage = 0;

    //添加点击事件

    [pageControl addTarget:self action:@selector(pageTurn:) forControlEvents:UIControlEventValueChanged];

    self.pageControl = pageControl;

    //添加到视图

    [self.view insertSubview:self.pageControl atIndex:3];

}

#pragma mark -- PageContorl method

- (void)pageTurn:(UIPageControl *)pageControl {

    NSInteger whichPage = pageControl.currentPage;

    [UIView setAnimationDuration:0.3];

    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

    self.scrollView.contentOffset = CGPointMake(CGRectGetWidth(self.view.bounds) * whichPage, 0.0);

}

#pragma mark -- scrollView methods

- (void)dynamicLoadImage {

for (int i = 0; i < IMAGE_COUNT; i ++) {

NSInteger index = (i - 1 + self.currentIndex + self.imageNameArray.count) % self.imageNameArray.count;

NSString *path = [[NSBundle mainBundle] pathForAuxiliaryExecutable:self.imageNameArray[index]];

UIImage *image = [UIImage imageWithContentsOfFile:path];

[self.imageViewArray[i] setImage:image];

self.scrollView.contentOffset = CGPointMake(CGRectGetWidth(self.view.bounds), 0);

}

}

- (void)pageLeft {

self.currentIndex = (--self.currentIndex + self.imageNameArray.count) % self.imageNameArray.count;

[self dynamicLoadImage];

}

- (void)pageRight {

self.currentIndex = (++self.currentIndex + self.imageNameArray.count) % self.imageNameArray.count;

[self dynamicLoadImage];

}

#pragma mark - UIScrollViewDelegate Method

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

    if (scrollView.contentOffset.x <=0) {

        [self pageLeft];

    } else if (scrollView.contentOffset.x >= CGRectGetWidth(scrollView.bounds) * 2) {

        [self pageRight];

    }

 self.pageControl.currentPage = self.currentIndex;

}

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

[self pauseTimer];

}

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

[self startTimer];

}

#pragma mark -- timer methods

- (void)startTimer {

if (!_timer) {

_timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(handleTimer:) userInfo:nil repeats:YES];

}

_timer.fireDate = [NSDate dateWithTimeIntervalSinceNow:2];

}

- (void)pauseTimer {

_timer.fireDate = [NSDate distantFuture];

}

- (void)stopTimer {

[_timer invalidate];

_timer = nil;

}

- (void)handleTimer:(NSTimer *)timer {

[self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];

}

- (void)viewWillAppear:(BOOL)animated

{

[super viewWillAppear:animated];

[self startTimer];

}

- (void)viewDidDisappear:(BOOL)animated

{

[super viewDidDisappear:animated];

[self stopTimer];

}

@end

附上demo

链接:http://pan.baidu.com/s/1nt5VzRj 密码:xmr9

ios PageControl and UIScrollView的更多相关文章

  1. 【iOS系列】-UIScrollView的介绍及结合UIPageControl实现图片播放的实例

    [iOS系列]-UIScrollView的介绍及结合UIPageControl实现图片播放的实例 第一:UIScrollView的常用属性 //表示UIScrollView内容的尺寸,滚动范围 @pr ...

  2. iOS开发之 UIScrollView的frame、contentSize、contentOffset和contentInset属性

    ios中下拉图片变大效果 http://blog.csdn.net/mad2man/article/details/14169197 IOS中UIScrollView的frame.contentSiz ...

  3. IOS UIView子类UIScrollView

    转自:http://www.cnblogs.com/nightwolf/p/3222597.html 虽然apple在IOS框架中提供了很多可以直接使用的UI控件,但是在实际开发当中我们通常都是要自己 ...

  4. IOS中的UIScrollView

    要引用UIScrollView 首先要遵循UIScrollViewDelegate协议 然后重写 //1.拖拽方法 -(void)scrollViewDidScroll:(UIScrollView * ...

  5. iOS开发基础-UIScrollView实现图片缩放

    当用户在 UIScrollView 上使用捏合手势时, UIScrollView 会给 UIScrollViewDelegate 协议发送一条消息,并调用代理的 viewForZoomingInScr ...

  6. iOS开发基础-UIScrollView基础

     普通的 UIView 不具备滚动功能,不能显示过多的内容.UIScrollView 是一个能够滚动的视图控件,可用来展示大量的内容.  UIScrollView 的简单使用: 1)将需要展示的内容添 ...

  7. IOS 怎么用UIScrollView来滚动和缩放他的内容第一篇

    本篇文章来自于互联网资料翻译 UIScrollView是在IOS最有用的控件之一.他是一个来展现超过一个屏幕的内容的很好的方式.下面有很多的技巧来使用他. 这篇文章就是关于UIScrollView的, ...

  8. IOS开发之UIScrollView约束布局

    概要 在iOS开发学习中,UIScrollView是绕不过去的一个重要控件. 但是相对于Android的ScrollView,iOS的这个滚动控件的用法简直是复杂一万倍... 最主要是目前能找到的大部 ...

  9. iOS学习之UIScrollView

    一.UIScrollView的创建和常用属性      1.UIScrollView概述 UIScrollView是UIView的子类. UIScrollView作为所有滚动视图的基类. UIScro ...

随机推荐

  1. spring mvc 之初体验

    Spring MVC最简单的配置 配置一个Spring MVC只需要三步: 在web.xml中配置Servlet: 创建Spring MVC的xml配置文件: 创建Controller和View &l ...

  2. 创建node.js,blog

    terminal npm init //创建项目 npm install --save express //安装 express 模块 npm install --save body-parser / ...

  3. Mac 升级node与npm

    第一步,先查看本机node.js版本: node -v 第二步,清除node.js的cache: sudo npm cache clean -f 第三步,安装 n 工具,这个工具是专门用来管理node ...

  4. byte字节数组的压缩

    写入内容到文件 public static void writeBytesToFile() throws IOException{ String s = "aaaaaaaaD等等" ...

  5. 高质量的C++博客

    陈硕  :http://blog.csdn.net/Solstice 孟岩: http://blog.csdn.net/myan

  6. 配置Eclipse可以查看JDK类库源码

    一.配置方法 配置Eclipse可以查看JDK类库源码 Window->Preferences->Java->Installed JREs 若没有JRE,需要自己添加进来,有的话,点 ...

  7. springmvc的@Validated/@Valid注解使用和BindingResult bindingResult

    关于@Valid和Validated的比较 @Valid是使用hibernate validation的时候使用 @Validated 是只用spring  Validator 校验机制使用 一:@V ...

  8. 设计模式--抽象工厂模式C++实现

    抽象工厂模式C++实现 1定义 为创建一组相关或者依赖的对象提供一个接口,且无需指定他们的具体类 2类图 3实现 class AbstractProduct { protected: Abstract ...

  9. uva10600次小生成树模板题

    裸题,上模板就行,注意j  !  =  k #include<map> #include<set> #include<cmath> #include<queu ...

  10. crontab执行定时任务

    在linux下面使用命令crontab -e  编辑任务: [adv@localhost]$ crontab -e 之后开始编辑任务 * * * * * cd /home/adv/work/cutte ...