图片轮播器[ScrollView,PageControl]
2015-09-03 15:23
225 查看
//
// ViewController.m
// 图片轮播器
//
// Created by qianfeng on 15/8/6.
// Copyright (c) 2015年
坏叔叔. All rights reserved.
//
#import "ViewController.h"
@interface
ViewController () <UIScrollViewDelegate>
{
NSInteger _currentPage;
}
@property (strong,
nonatomic) UIPageControl *pageControl;
@property (nonatomic,
strong) UIScrollView *scrollView;
@property (strong,
nonatomic) NSTimer *timer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super
viewDidLoad];
// 1. 创建ScrollView、Pagecontrol
[self
createScrollViewAndPageControl];
// 2. 启动定时器
[self
addTimer];
}
- (void)createScrollViewAndPageControl {
self.scrollView = [[UIScrollView
alloc] init];
self.scrollView.frame =
CGRectMake(10,
20, 300,
150);
self.scrollView.backgroundColor = [UIColor
blueColor];
[self.view
addSubview:self.scrollView];
//
设置代理
self.scrollView.delegate =
self;
CGFloat imageW =
self.scrollView.frame.size.width;
CGFloat imageH =
self.scrollView.frame.size.height;
CGFloat imageY = 0;
for (int i =
0; i < 5; i++) {
UIImageView *imageView = [[UIImageView
alloc] init];
CGFloat imageX = i * imageW;
imageView.frame =
CGRectMake(imageX, imageY, imageW, imageH);
// 获取图片名
NSString *name = [NSString
stringWithFormat:@"img_0%d",i+1];
imageView.image = [UIImage
imageNamed:name];
[self.scrollView
addSubview:imageView];
}
self.scrollView.contentSize =
CGSizeMake(imageW *
5, imageH);
self.scrollView.showsHorizontalScrollIndicator =
NO;
self.scrollView.pagingEnabled =
YES;
//
添加pageControl
self.pageControl = [[UIPageControl
alloc] initWithFrame:CGRectMake(10,
150,
300, 20)];
[self.pageControl
setBackgroundColor:[UIColor
clearColor]]; //
清除背景色
self.pageControl.pageIndicatorTintColor = [UIColor
grayColor];
self.pageControl.numberOfPages =
5;
_currentPage =
self.pageControl.numberOfPages;
[self.view
addSubview:self.pageControl];
//
调用点击pagecontrol上的点
方法
[self
performSelector:@selector(clickedPoint)
withObject:nil
afterDelay:0.5];
[self.pageControl
addTarget:self
action:@selector(pageControlTouched:)
forControlEvents:UIControlEventTouchUpInside];
}
/**
* 点击pagecontrol上的点可以调整图片显示
*/
- (void)clickedPoint {
for (int i =
0; i < self.pageControl.subviews.count; i++) {
UIView *oneView = [self.pageControl.subviews
objectAtIndex:i];
oneView.tag =
10 + i;
// NSLog(@"%@", oneView);
// 添加手势
UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer
alloc]
initWithTarget:self
action:@selector(tapGR:)];
[oneView
addGestureRecognizer:tapGR];
}
}
- (void)tapGR:(UITapGestureRecognizer *)tapGR {
UIView *theView = tapGR.view;
self.pageControl.currentPage = theView.tag -
10;
CGPoint offset =
CGPointMake(self.scrollView.frame.size.width
* self.pageControl.currentPage,
0);
[self.scrollView
setContentOffset:offset animated:YES];
}
/**
* 页面之间的滑动及转换
*/
- (void)pageControlTouched:(UIPageControl *)pageControl {
if (pageControl.currentPage ==
0 && _currentPage ==
0) {
pageControl.currentPage =
4;
}else
if (pageControl.currentPage ==
4 && _currentPage ==
4){
pageControl.currentPage =
0;
}
_currentPage = pageControl.currentPage;
CGPoint offset = CGPointMake(self.scrollView.frame.size.width
* pageControl.currentPage,
0);
[self.scrollView
setContentOffset:offset animated:YES];
}
/**
* 代理方法
在拖拽的时候暂停定时器并释放timer
*/
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
[self.timer
invalidate];
self.timer =
nil;
}
/**
* 代理方法
在拖拽的结束启动定时器
*/
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
// 1.
启动定时器
[self
addTimer];
}
/**
* 代理方法
*/
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (self.timer) {
return;
}
self.pageControl.currentPage = (scrollView.contentOffset.x + scrollView.frame.size.width
* 0.5)/ scrollView.frame.size.width;
}
- (void)addTimer
// 定时器
{
// self.timer = [NSTimer timerWithTimeInterval:2 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
// [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
self.timer = [NSTimer
scheduledTimerWithTimeInterval:2.0
target:self
selector:@selector(nextImage)
userInfo:nil
repeats:YES];
}
- (void)nextImage
{
// 1.下一页
if (self.pageControl.currentPage ==
4) {
self.pageControl.currentPage =
0;
}
else {
self.pageControl.currentPage++;
}
// 2.设置滚动
CGPoint offset =
CGPointMake(self.scrollView.frame.size.width
* self.pageControl.currentPage,
0);
[self.scrollView
setContentOffset:offset animated:YES];
}
@end
// ViewController.m
// 图片轮播器
//
// Created by qianfeng on 15/8/6.
// Copyright (c) 2015年
坏叔叔. All rights reserved.
//
#import "ViewController.h"
@interface
ViewController () <UIScrollViewDelegate>
{
NSInteger _currentPage;
}
@property (strong,
nonatomic) UIPageControl *pageControl;
@property (nonatomic,
strong) UIScrollView *scrollView;
@property (strong,
nonatomic) NSTimer *timer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super
viewDidLoad];
// 1. 创建ScrollView、Pagecontrol
[self
createScrollViewAndPageControl];
// 2. 启动定时器
[self
addTimer];
}
- (void)createScrollViewAndPageControl {
self.scrollView = [[UIScrollView
alloc] init];
self.scrollView.frame =
CGRectMake(10,
20, 300,
150);
self.scrollView.backgroundColor = [UIColor
blueColor];
[self.view
addSubview:self.scrollView];
//
设置代理
self.scrollView.delegate =
self;
CGFloat imageW =
self.scrollView.frame.size.width;
CGFloat imageH =
self.scrollView.frame.size.height;
CGFloat imageY = 0;
for (int i =
0; i < 5; i++) {
UIImageView *imageView = [[UIImageView
alloc] init];
CGFloat imageX = i * imageW;
imageView.frame =
CGRectMake(imageX, imageY, imageW, imageH);
// 获取图片名
NSString *name = [NSString
stringWithFormat:@"img_0%d",i+1];
imageView.image = [UIImage
imageNamed:name];
[self.scrollView
addSubview:imageView];
}
self.scrollView.contentSize =
CGSizeMake(imageW *
5, imageH);
self.scrollView.showsHorizontalScrollIndicator =
NO;
self.scrollView.pagingEnabled =
YES;
//
添加pageControl
self.pageControl = [[UIPageControl
alloc] initWithFrame:CGRectMake(10,
150,
300, 20)];
[self.pageControl
setBackgroundColor:[UIColor
clearColor]]; //
清除背景色
self.pageControl.pageIndicatorTintColor = [UIColor
grayColor];
self.pageControl.numberOfPages =
5;
_currentPage =
self.pageControl.numberOfPages;
[self.view
addSubview:self.pageControl];
//
调用点击pagecontrol上的点
方法
[self
performSelector:@selector(clickedPoint)
withObject:nil
afterDelay:0.5];
[self.pageControl
addTarget:self
action:@selector(pageControlTouched:)
forControlEvents:UIControlEventTouchUpInside];
}
/**
* 点击pagecontrol上的点可以调整图片显示
*/
- (void)clickedPoint {
for (int i =
0; i < self.pageControl.subviews.count; i++) {
UIView *oneView = [self.pageControl.subviews
objectAtIndex:i];
oneView.tag =
10 + i;
// NSLog(@"%@", oneView);
// 添加手势
UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer
alloc]
initWithTarget:self
action:@selector(tapGR:)];
[oneView
addGestureRecognizer:tapGR];
}
}
- (void)tapGR:(UITapGestureRecognizer *)tapGR {
UIView *theView = tapGR.view;
self.pageControl.currentPage = theView.tag -
10;
CGPoint offset =
CGPointMake(self.scrollView.frame.size.width
* self.pageControl.currentPage,
0);
[self.scrollView
setContentOffset:offset animated:YES];
}
/**
* 页面之间的滑动及转换
*/
- (void)pageControlTouched:(UIPageControl *)pageControl {
if (pageControl.currentPage ==
0 && _currentPage ==
0) {
pageControl.currentPage =
4;
}else
if (pageControl.currentPage ==
4 && _currentPage ==
4){
pageControl.currentPage =
0;
}
_currentPage = pageControl.currentPage;
CGPoint offset = CGPointMake(self.scrollView.frame.size.width
* pageControl.currentPage,
0);
[self.scrollView
setContentOffset:offset animated:YES];
}
/**
* 代理方法
在拖拽的时候暂停定时器并释放timer
*/
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
[self.timer
invalidate];
self.timer =
nil;
}
/**
* 代理方法
在拖拽的结束启动定时器
*/
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
// 1.
启动定时器
[self
addTimer];
}
/**
* 代理方法
*/
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (self.timer) {
return;
}
self.pageControl.currentPage = (scrollView.contentOffset.x + scrollView.frame.size.width
* 0.5)/ scrollView.frame.size.width;
}
- (void)addTimer
// 定时器
{
// self.timer = [NSTimer timerWithTimeInterval:2 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
// [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
self.timer = [NSTimer
scheduledTimerWithTimeInterval:2.0
target:self
selector:@selector(nextImage)
userInfo:nil
repeats:YES];
}
- (void)nextImage
{
// 1.下一页
if (self.pageControl.currentPage ==
4) {
self.pageControl.currentPage =
0;
}
else {
self.pageControl.currentPage++;
}
// 2.设置滚动
CGPoint offset =
CGPointMake(self.scrollView.frame.size.width
* self.pageControl.currentPage,
0);
[self.scrollView
setContentOffset:offset animated:YES];
}
@end
相关文章推荐
- A Rose by Any Other Name Will End Up As a Cabbage
- KMP 模板
- Java记录 -19- 单例模式
- 通达OA 小飞鱼工作流在线培训教程(八)常用表单控件
- 通达OA 小飞鱼工作流在线培训教程(八)常用表单控件
- If You Design It, You Should Be Able to Code It
- 【阿里笔试+蘑菇街面试】向来缘浅,奈何情深
- 布线问题
- Linux之文件系统管理
- phalapi改动的地方
- nyoj 327 亲和串 【kmp(好题)】
- 关于Android开发中Java对象序列化的一次测试
- Stretch Key Dimensions to See What Breaks
- 频道发布与消息订阅
- Objective-C 【This is ARC】
- KVO实现机制 & 如何自己动手实现 KVO
- Objective-C 【This is ARC】
- E. Bear and Drawing
- ubifs烧写遇到《UBI error: ubi_io_read: error -1 while reading 64 bytes from PEB》
- sass 安装使用杂记