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 ...
随机推荐
- bat批处理以当前时间创建文本文件
:: 表示注释 :: @表示不显示当前命令,只在后台执行 :: @echo off 表示以后执行的命令都不显示 :: set d=%,% 表示设置变量d为当前年月日,默认表示为例如:// :: set ...
- 【转】GitHub汉化脚本(谷歌浏览器)
// ==UserScript== // @name GitHub 汉化插件 // @description 汉化 GitHub 界面的部分菜单及内容. // @copyright 2016, 楼教主 ...
- Pandas 高级应用 数据分析
深入pandas 数据处理 三个阶段 数据准备 数据转化 数据聚合 数据准备 加载 组装 合并 - pandas.merge() 拼接 - pandas.concat() 组合 - pandas.Da ...
- C# 自动触发鼠标、键盘事件
要在C#程序中触发鼠标.键盘事件必须要调用windows函数. 一.鼠标事件的触发 1.引用windows函数mouse_event /// <summary> /// 鼠标事件 /// ...
- js 光标选中 操作
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- css 固定宽度,自动换行
max-width: 200px; display: block; word-break: break-all:
- 聊一聊Spring AOP
前两天,在给新入职的同事做技术介绍时,讲到spring的AOP.使我又一次认识到,对于AOP,特别是spring AOP的理解,虽然大家都能说上来几句,但是许多人认识并不太全面,甚至可以说是一知半解- ...
- Vlmcsd(KMS)激活服务器程序
1.下载vlmcsd程序 2-1.虚拟机版本: 新建Linux虚拟机,硬件仅保留内存(最小14MB,推荐16MB).处理器(1个1核心).软盘(指向floppy144.flp).网络适配器(桥接模式) ...
- S16 day7 socket
网络基础之网络协议篇:http://www.cnblogs.com/linhaifeng/articles/5937962.html 参考博客:http://www.cnblogs.com/linha ...
- vue项目搭建 (二) axios 封装篇
vue项目搭建 (二) axios 封装篇 项目布局 vue-cli构建初始项目后,在src中进行增删修改 // 此处是模仿github上 bailicangdu 的 ├── src | ├── ap ...