ios PageControl and UIScrollView
//
// 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的更多相关文章
- 【iOS系列】-UIScrollView的介绍及结合UIPageControl实现图片播放的实例
[iOS系列]-UIScrollView的介绍及结合UIPageControl实现图片播放的实例 第一:UIScrollView的常用属性 //表示UIScrollView内容的尺寸,滚动范围 @pr ...
- iOS开发之 UIScrollView的frame、contentSize、contentOffset和contentInset属性
ios中下拉图片变大效果 http://blog.csdn.net/mad2man/article/details/14169197 IOS中UIScrollView的frame.contentSiz ...
- IOS UIView子类UIScrollView
转自:http://www.cnblogs.com/nightwolf/p/3222597.html 虽然apple在IOS框架中提供了很多可以直接使用的UI控件,但是在实际开发当中我们通常都是要自己 ...
- IOS中的UIScrollView
要引用UIScrollView 首先要遵循UIScrollViewDelegate协议 然后重写 //1.拖拽方法 -(void)scrollViewDidScroll:(UIScrollView * ...
- iOS开发基础-UIScrollView实现图片缩放
当用户在 UIScrollView 上使用捏合手势时, UIScrollView 会给 UIScrollViewDelegate 协议发送一条消息,并调用代理的 viewForZoomingInScr ...
- iOS开发基础-UIScrollView基础
普通的 UIView 不具备滚动功能,不能显示过多的内容.UIScrollView 是一个能够滚动的视图控件,可用来展示大量的内容. UIScrollView 的简单使用: 1)将需要展示的内容添 ...
- IOS 怎么用UIScrollView来滚动和缩放他的内容第一篇
本篇文章来自于互联网资料翻译 UIScrollView是在IOS最有用的控件之一.他是一个来展现超过一个屏幕的内容的很好的方式.下面有很多的技巧来使用他. 这篇文章就是关于UIScrollView的, ...
- IOS开发之UIScrollView约束布局
概要 在iOS开发学习中,UIScrollView是绕不过去的一个重要控件. 但是相对于Android的ScrollView,iOS的这个滚动控件的用法简直是复杂一万倍... 最主要是目前能找到的大部 ...
- iOS学习之UIScrollView
一.UIScrollView的创建和常用属性 1.UIScrollView概述 UIScrollView是UIView的子类. UIScrollView作为所有滚动视图的基类. UIScro ...
随机推荐
- tsar的使用
项目地址https://github.com/alibaba/tsar 安装 $ git clone git://github.com/kongjian/tsar.git $ cd tsar $ ma ...
- java.lang.IllegalArgumentException的解决方法
java.lang.IllegalArgumentException这个错误基本上就是jdk版本的问题 把jdk1.8换成jdk1.7就可以了 这里可以设置jdk最低版本 这里默认要选择jdk1.7 ...
- Spring Cloud 坑点
1 配置中心 1.config 默认Git加载 通过spring.cloud.config.server.git.uri指定配置信息存储的git地址,比如:https://github.com/spr ...
- spring boot2.1读取 apollo 配置中心3
上篇记录了springboot读取apollo的配置信息,以及如何获取服务端的推送更新配置. 接下来记录一下,如何获取公共namespace的配置. 上文中使用如下代码共聚公共命名空间的配置: @Ap ...
- pathway一些网站
1.BioCarta_Pathways https://cgap.nci.nih.gov/Pathways/BioCarta_Pathways
- LInux50个基本命令
cd:(切换)vim:(创建文件) vi:编辑文件bc:(计算器)quit:退出计算器mkdir:(创建目录) mkdir -p:递归建立目录rmdir:(删除目录)arch:(显示处理器X8 ...
- 2-4-搭建DHCP服务实现动态分配IP地址-NTP网络时间同步
本节所讲内容: •DHCP服务器工作原理 •使用DHCP为局域网中的机器分配IP地址 •使用DHCP为服务器分配固定IP地址 •ntpdate加计划任务同步服务器时间 ---------------- ...
- JavaScript---js的模块化
js的模块模式被定义为给类提供私有和公共封装的一种方法,也就是我们常说的“模块化”. 怎么实现“模块化”? 通过闭包的原理来实现“模块化” ,具体实现:1.必须有外部的封闭函数,该函数必须至少被调用 ...
- 【转】Fork/Join框架测试
Fork/Join框架介绍 下面使用该框架计算0-50000000000的和,并比较普通计算方法.Fork/Join框架.Java8新特性三种计算方式的计算时间: import java.time.D ...
- 网络编程之socketserver初识
网络编程之socketserver初识 Server #!/usr/bin/env python # @Author : "Wjl" # @Date : 2017/12/22 # ...