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 ...
随机推荐
- Nodejs Q promise设计思路
Nodejs Q promise库 前言 Q库为nodejs提供了一个基于promise的编程方式,从此避免了一层又一层的callback调用.不过Q的灵活性也给我造成了很大困扰,我可以用promis ...
- jsp选项卡导航实现——模板
效果 刚进来页面的样子 在第二个选项卡上方时 点击后 离开 同样第三个 点击 移走鼠标 代码 <%@ page contentType="text/html;charset=UTF-8 ...
- Windows下实现mysql定时备份
1.写MySQL备份bat处理 @echo off set "yMd=%date:~,4%%date:~5,2%%date:~8,2%" set "hms=%time:~ ...
- class.getResource()和getResourceAsStream的用法
转自:http://blog.csdn.net/lcj8/article/details/3502849 class.getResource()的用法 用JAVA获取文件,听似简单,但对于很多像我这样 ...
- 查看linux 之mysql 是否安装的几种方法
转自:https://jingyan.baidu.com/album/86112f1378bf282737978730.html?picindex=2 linux下怎么启动mysql服务 https: ...
- ”由于没有远程桌面授权服务器可以提供许可证,远程会话被中断“的解决方案
由于windows server 2012 R2 Datacenter 安装了 远程桌面角色,但是这个角色是120天免费的,需要购买授权的. 解决方案: 删除这个角色,就可以正常进行远程桌面连接了.但 ...
- cocos2d-js入门一
决定搞cocos2d-js,但发现官网已经没有独立的js了,lua,现在全部整合到cocos2d-x中了. win7+cocos2d-x 3.8 由于之前搭建了vs2012 +python平台 ,此时 ...
- Vuex访问状态对象的方法
除了<Vuex最基本样例>中的方法外,还有两种方法访问状态对象state: 只需要改app.vue文件 方法一:引入computed <template> <div id ...
- angular $q的学习笔记转帖
http://blog.segmentfault.com/bornkiller/1190000000402555 angular $q的一个不错的学习笔记
- Java的序列化机制
1. 所有实现序列化的类都必须实现Serializable接口,序列化有如下两个特点: 如果一个类可以被序列化,那么它的子类也可以被序列化 由于static代表类成员,trasient代表对象的临时数 ...