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 ...
随机推荐
- 【卷积神经网络】对BN层的解释
前言 Batch Normalization是由google提出的一种训练优化方法.参考论文:Batch Normalization Accelerating Deep Network Trainin ...
- QT 样式表实例
目标:实现button的圆角效果及背景颜色,鼠标滑过颜色变亮,鼠标点击颜色变重. 总体思路首,先根据需要及样式规则新建.qss文件,然后在代码中将文件引用并应用样式. 具体过程如下: 1在项目当前目录 ...
- Android开发-ADT Bundle安装
此次安装是在Android Studio的机器上.安装Eclipse是因为目前很多Android程序是Eclipse开发的,要想快速看到运行效果就是安装Eclipse. 1.所有的资源都在:http: ...
- Normalize.css与CSS reset区别
Normalize.css 只是一个很小的CSS文件,但它在默认的HTML元素样式上提供了跨浏览器的高度一致性.相比于传统的CSS reset,Normalize.css是一种现代的.为HTML5准备 ...
- 服务器22端口连接超时 ssh: connect to host *** port 22: Operation timed out
最近酸酸乳出问题,连接v社服务器发现碰到 ssh: connect to host master port 22: Connection timed out 的问题.现在对该问题做一下可能出现的问题 ...
- 学习 nginx (持续更新)
什么是代理与反向代理,有什么应用场景? 平常经常听别人说代理与反向代理,那么这二者到底有什么区别呢? 代理 场景:我需要访问一个服务器C,但是由于某些原因我无法访问到它,(典型的就是你FQ,然后fai ...
- Python之如何删除pandas DataFrame的某一/几列
删除pandas DataFrame的某一/几列: 方法一:直接del DF['column-name'] 方法二:采用drop方法,有下面三种等价的表达式: 1. DF= DF.drop('co ...
- MyBatis的返回参数类型
MyBatis的返回参数类型分两种 1. 对应的分类为: 1.1.resultMap: 1.2.resultType: 2 .对应返回值类型: 2.1.resultMap:结果集 2.2.result ...
- Linux 策略路由配置
策略路由配置 #编辑rt_tables echo "192 net_192 " >> /etc/iproute2/rt_tables echo "196 ne ...
- 006-对象—— static关键字 静态属性和方法的使用
<?php /*static()静态属性: */ //静态属性: /*class Model{ private $mysqli; static $config;//数据库连接状态 functio ...